如何使用正则表达式删除字符串的一个非常特定的部分:

时间:2017-03-25 12:26:24

标签: python regex

我想知道如何使用正则表达式删除字符串的特定部分。例如,我有一个如下所示的列表:

name=derek;
name=derek.1;
name=derek.2; 
name=derek.3;
name=ophelia;
name=ophelia.1;
name=ophelia.2;
name=ophelia.3;

我想替换'name ='之后和'。'之间的字符。如果存在或';'在行尾。输出应该如下所示:

name=apple;
name=apple.1;
name=apple.2; 
name=apple.3;
name=apple;
name=apple.1;
name=apple.2;
name=apple.3;

2 个答案:

答案 0 :(得分:1)

你需要一个多行正则表达式:

>>> print(s)
name=derek;
name=derek.1;
name=derek.2; 
name=derek.3;
name=ophelia;
name=ophelia.1;
name=ophelia.2;
name=ophelia.3;
>>> replace_with = r'\1{0}\2'.format('apple')  # preserve start & end
                                               # replace only the middle
>>> regex = '^(name=)[^\.;]+(.*)$'
>>> replaced = re.sub(regex,                # pattern
...                   replace_with,         # replacement
...                   s,                    # an original multiline string
...                   flags=re.MULTILINE);  # regex for multiple lines
>>> print(replaced)
name=apple;
name=apple.1;
name=apple.2; 
name=apple.3;
name=apple;
name=apple.1;
name=apple.2;
name=apple.3;

答案 1 :(得分:0)

这并不是您要求的,但这里的版本根本不需要正则表达式:

s = """name=derek;
name=derek.1;
name=derek.2; 
name=derek.3;
name=ophelia;
name=ophelia.1;
name=ophelia.2;
name=ophelia.3;"""

lines = s.split("\n")
updated = []
for line in lines:
    left, right = line.split("=")
    if "." in right:
        new_right = "apple." + right.split(".")[1]
    else:
        new_right = "apple;"
    updated.append(left + "=" + new_right)

print "\n".join(updated)