从字符串中删除一串非特定字符的方法

时间:2017-06-20 18:19:10

标签: python

我正在尝试根据标记转换输入字符串。

因此,("Hail Mary #Run quick see\ncd!sawce", ["#", "!"])将删除标记之后的所有字符,但直到反斜杠或字符串结束。

该案件将成为("Hail Mary #Run quick see\ncd!sawce", ["#", "!"])

  
    

Hail Mary cd

  

我写了这个:

def solution(string, markers):
    orig = string.strip()
    wholeList = []
    newString = orig
    for char in orig:
        for j in markers:
            if char == j:
                i = orig.index(char)
                newString = orig[:i].strip()
                wholeList.append(newString)
        if char == "\\":
            i = orig.index(char)
            orig = orig[i:]

return "".join(wholeList)

2 个答案:

答案 0 :(得分:3)

首先,如果您打算在字符串文字中使用文字反斜杠,则需要转义反斜杠。

然后,您可以使用正则表达式替换:

import re
s = "Hail Thor #Run quick see\\ncd!sawce"
print( re.sub( r"[#!].*?(?=[\\]|$)", "", s) )   # Hail Thor \ncd

如果您打算使用换行符\n,并且希望删除直到下一个换行符,那么它就更容易了,因为默认情况下.与换行符不匹配:

import re
s = "Hail Thor #Run quick see\ncd!sawce"
print( re.sub( r"[#!].*", "", s) )   # Hail Thor
                                     # cd

答案 1 :(得分:0)

虽然我同意@trincot使用正则表达式,但我提出了另一种解决方案:

def solution(text, markers):
    # If all the markers are used or
    # there are no markers return text
    if len(markers) < 1:
        return text.strip()
    else:
        # Get the first marker
        marker = markers.pop(0)
        # Split the string with the marker
        before, after = text.split(marker)
        # Split the remaining string at a newline
        after = after.split("\n", 1)
        if len(after) > 1:
            end = "\n" + after[-1]
        else:
            end = ""
        text = before.strip() + end
        return solution(text, markers)