Parsing strings in python

So my problem is this, I have a file that looks like this:

[SHIFT]this isrd[BACKSPACE][BACKSPACE] an example file[SHIFT]1

This, of course, translate into

' This is an example file!'

I am looking for a way to parse the source content into the final content, so that it [BACKSPACE]will remove the last character (spaces are included), and multiple backspaces will delete some characters. [SHIFT]for me it does not really matter. Thanks for the help!

+5
source share
5 answers

Here is one way, but it feels like a hacker. Probably the best way.

def process_backspaces(input, token='[BACKSPACE]'):
    """Delete character before an occurence of "token" in a string."""
    output = ''
    for item in (input+' ').split(token):
        output += item
        output = output[:-1]
    return output

def process_shifts(input, token='[SHIFT]'):
    """Replace characters after an occurence of "token" with their uppecase 
    equivalent. (Doesn't turn "1" into "!" or "2" into "@", however!)."""
    output = ''
    for item in (' '+input).split(token):
        output += item[0].upper() + item[1:]
    return output

test_string = '[SHIFT]this isrd[BACKSPACE][BACKSPACE] an example file[SHIFT]1'
print process_backspaces(process_shifts(test_string))
+1
source

If you don't need shifts, just split them, download

(defun apply-bspace ()
  (interactive)
  (let ((result (search-forward "[BACKSPACE]")))
    (backward-delete-char 12)
    (when result (apply-bspace))))

M-x apply-bspace . Elisp, python, " -, ".

: Shift , ( [SHIFT]2 = > @, [SHIFT]3 = > # ..). -

(defun apply-shift ()
  (interactive)
  (let ((result (search-forward "[SHIFT]")))
    (backward-delete-char 7)
    (upcase-region (point) (+ 1 (point)))
    (when result (apply-shift))))
+1

, :

def shift(s):
    LOWER = '`1234567890-=[];\'\,./'
    UPPER = '~!@#$%^&*()_+{}:"|<>?'

    if s.isalpha():
        return s.upper()
    else:
        return UPPER[LOWER.index(s)]

def parse(input):
    input = input.split("[BACKSPACE]")
    answer = ''
    i = 0
    while i<len(input):
        s = input[i]
        if not s:
            pass
        elif i+1<len(input) and not input[i+1]:
            s = s[:-1]
        else:
            answer += s
            i += 1
            continue
        answer += s[:-1]
        i += 1

    return ''.join(shift(i[0])+i[1:] for i in answer.split("[SHIFT]") if i)

>>> print parse("[SHIFT]this isrd[BACKSPACE][BACKSPACE] an example file[SHIFT]1")
>>> This is an example file!
+1

, (-) [BACKSPACE] ...

re.sub('.?\[BACKSPACE\]', '', YourString.replace('[SHIFT]', ''))

, " ", .

0

, , .

:

# -*- coding: utf-8 -*-

import re

upper_value = {
    1: '!', 2:'"',
}

tokenizer = re.compile(r'(\[.*?\]|.)')
origin = "[SHIFT]this isrd[BACKSPACE][BACKSPACE] an example file[SHIFT]1"
result = ""

shift = False

for token in tokenizer.findall(origin):
    if not token.startswith("["):
        if(shift):
            shift = False
            try:
                token = upper_value[int(token)]
            except ValueError:
                token = token.upper()

        result = result + token
    else:
        if(token == "[SHIFT]"):
            shift = True
        elif(token == "[BACKSPACE]"):
            result = result[0:-1]

, , , .

, : -)

0

All Articles