根据特定模式和编辑字符串进行分割

时间:2019-07-14 16:08:43

标签: python regex split pattern-matching

我正在尝试根据特定的模式拆分字符串,以便稍后添加一些字符后重新加入。

这是我的字符串示例:“ 123 \ babc \ b:123”,我需要将其转换为“ 123 \ babc \\” b \“:123”。我需要在长字符串中多次执行此操作。我尝试了以下变化:

regex = r"(\\b[a-zA-Z]+)\\b:"
test_str = "123\\babc\\b:123"
x = re.split(regex, test_str)

但是我加入的位置并不正确。是否有另一种方式可以做到这一点/另一种拆分和联接方式?

2 个答案:

答案 0 :(得分:1)

您是对的,您可以按照建议使用re.split进行操作。您可以将\b分开,然后使用特定的分隔符重建输出(并在需要时保留\b)。

这里有个例子:

# Import module
import re

string = "123\\babc\\b:123"

# Split by "\n"
list_sliced = re.split(r'\\b', "123\\babc\\b:123")
print(list_sliced)
# ['123', 'abc', ':123']

# Define your custom separator
custom_sep = '\\\\"b\\"'
# Build your new output
output = list_sliced[0]
# Iterate over each word
for i, word in enumerate(list_sliced[1:]):
    # Chose the separator according the parity (since we don't want to change the first "\b")
    sep = "\\\\b"
    if i % 2 ==  1:
        sep = custom_sep
    # Update output
    output += sep + word

print(output)
# 123\\babc\\"b\":123

答案 1 :(得分:0)

也许下面的表达式

^([\\]*)([^\\]+)([\\]*)([^\\]+)([\\]*)([^:]+):(.*)$

并替换为

\1\2\3\4\5\\"\6\\":\7
带有re.sub

可能会返回我们想要的输出。

如果要探索/简化/修改表达式,请在this demo右上角进行解释。

相关问题