注意:我使用的是PyPi替代regex模块
我有一个python程序,我在其中寻找特定格式的重复标签,用逗号分隔。你知道吗
格式为:(*单词…*#*数字*)
例如:Trial #1, Trial #2, Run #3,
和Spring trial #13
都适合这个格式。你知道吗
我使用原始字符串中的:([\w ]*#\d\d?,)\1*
作为正则表达式模式。你知道吗
在java和各种正则表达式测试引擎中,对字符串使用findall()
和以下模式:
Run #1,Run #1,Run #1,Run #1,Run #1,Run #1,Run #1,Run #2,Run #2,Run #2,Run #2,Run #2,Run #2,Run #2,Run #3,Run #3,Run #3,Run #3,Run #3,Run #3,Run #3, (...
...) Run #20,Run #20,Run #20,Run #20,Run #20,Run #20,Run #20
退货:
match 1: Run #1,Run #1,Run #1,Run #1,Run #1,Run #1,Run #1,
match 2: Run #2,Run #2,Run #2,Run #2,Run #2,Run #2,Run #2,
...etc.
但在python中,它返回:
match 1: Run #1,
match 2: Run #2,
...etc.
我希望它返回第一个结果(java和其他程序的正则表达式返回的结果)
python的regex引擎有什么我忽略的地方吗?为什么我会得到这个结果?你知道吗
我的代码是:
import regex
file = open('Pendulum Data.csv',mode='r')
header1 = file.readline()
header2 = file.readline()
pattern1 = regex.compile(r'([\w ]*#\d\d?)\1*',flags=regex.V0)
header1Match = pattern1.findall(header1)
for x in header1Match:
print(x)
for循环和print语句用于查看结果。你知道吗
(这给我带来了另一个问题:regex.findall()
到底返回了什么?findall()
是不是在打印结果的时候返回了我想要的结果你知道吗
…是的,我正在用一个原始字符串作为我的模式。你知道吗
您正在正则表达式中使用捕获组。如果在模式中指定了捕获组,Python
.finall
将返回捕获文本的元组。因此,您正在寻找一个.finditer
函数。你知道吗见Python ^{} documentation :
和^{} :
下面是一个使用
re.finditer
的small demo:结果:
Casimir是对的,通过这样一个普通的正则表达式,您可以使用正则
re
模块。你知道吗相关问题 更多 >
编程相关推荐