在Python正则表达式中动态命名组
有没有办法在Python中动态更新正则表达式组的名称呢?
比如,如果文本是:
person 1: name1
person 2: name2
person 3: name3
...
person N: nameN
那你怎么给这些组命名,比如'person1'、'person2'、'person3',一直到'personN',而事先又不知道有多少个人呢?
4 个回答
1
命名捕获组和编号组(比如 \1, \2 等)不能动态变化,但你可以用 findall 来实现类似的效果:
re.findall(pattern, string[, flags])
这个函数会返回在字符串中与模式匹配的所有非重叠的结果,结果以字符串列表的形式返回。它会从左到右扫描字符串,找到的匹配项会按照顺序返回。如果模式中有一个或多个组,它会返回一个组的列表;如果模式有多个组,结果将是一个包含元组的列表。空匹配也会被包含在结果中,前提是它们不与其他匹配项的开头重叠。
1
根据你接受的答案,似乎不需要用到正则表达式。
p="""
person 1: name1
person 2: name2
person 3: name3
person N: nameN
"""
ARR=[]
for item in p.split("\n"):
if item:
s=item.split(":")
ARR.append(s)
print ARR
输出结果
$ ./python.py
[['person 1', ' name1'], ['person 2', ' name2'], ['person 3', ' name3'], ['person N', ' nameN']]
2
不可以,但你可以这样做:
>>> import re
>>> p = re.compile('(?m)^(.*?)\\s*:\\s*(.*)$')
>>> text = '''person 1: name1
person 2: name2
person 3: name3
...
person N: nameN'''
>>> p.findall(text)
输出结果:
[('person 1', 'name1'), ('person 2', 'name2'), ('person 3', 'name3'), ('person N', 'nameN')]
简单解释一下:
(?m) # enable multi-line mode
^ # match the start of a new line
(.*?) # un-greedily match zero or more chars and store it in match group 1
\s*:\s* # match a colon possibly surrounded by space chars
(.*) # match the rest of the line and store it in match group 2
$ # match the end of the line
参考资料