用于查找第二个数字组或在 python 中的字符串中开始搜索的正则表达式语法
Regex syntax for finding second digit group OR start of search in string in python
我正在尝试使用 Python 中的 Regex
来查找字符串中的第二组数字,格式为:
"123= TEXTINCAPS('Example One',$,$,10.,$);"
该格式还可以包含更长的数字组或值低于 1.0 的组:
"123= TEXTINCAPS('Example Two',$,$,0.2521,$);"
我期望得到的结果是:
10
和
0.2521
当我使用在线测试器查找“(\d+)”时,它找到了两组数字(即 123 和 10),但它在“匹配信息”面板中将它们都标记为 "Group 1" .
我的问题是:
1) 我怎样才能 select 只返回第 2 组?
2) 从字符串的某个点开始正则表达式是否更好,比如 '(' 字符?即使阅读了它,我也不确定该怎么做。
只需捕获模式中由任何非数字分隔的两组数字:
>>> import re
>>> s = "123= TEXTINCAPS('Example One',$,$,10.,$);"
>>> m = re.match(r'(\d+)[^\d]+(\d+)', s)
>>> m.group(1)
123
>>> m.group(2)
10
只找到所有个号码然后选择第二个!
所以你的第一个例子:
e1 = "123= TEXTINCAPS('Example One',$,$,10.,$);"
我们可以做到:
re.findall("\d*\.*\d+", e1)
给出:
["123", "10"]
还有:
e2 = "123= TEXTINCAPS('Example Two',$,$,0.2521,$);"
我们得到:
['123', '0.2521']
很明显,如果您只想要第二个索引,只需使用 re.findall(...)[1]
.
进行上述调用即可获取第二个元素
如果您的格式始终是函数调用的格式,您可以完全跳过正则表达式并以逗号分隔:
line = "123= TEXTINCAPS('Example One',$,$,10.,$);"
print(line.split(',')[-2])
(注意:首选负索引,因为前导引号字符串中可能有一个或多个逗号。)
You can use Positive Lookbehind (?<=,) :
import re
pattern=r'(?<=,)[0-9.]+'
text="""123= TEXTINCAPS('Example Two',$,$,0.2521,$);
"123= TEXTINCAPS('Example One',$,$,10.,$);"""
match=re.finditer(pattern,text)
for find in match:
print(find.group())
输出:
0.2521
10.
正则表达式解释:
Positive Lookbehind (?<=,)
Assert that the Regex below matches
, matches the character , literally (case sensitive)
Match a single character present in the list below [0-9.]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9
a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
. matches the character . literally (case sensitive)
我正在尝试使用 Python 中的 Regex
来查找字符串中的第二组数字,格式为:
"123= TEXTINCAPS('Example One',$,$,10.,$);"
该格式还可以包含更长的数字组或值低于 1.0 的组:
"123= TEXTINCAPS('Example Two',$,$,0.2521,$);"
我期望得到的结果是:
10
和
0.2521
当我使用在线测试器查找“(\d+)”时,它找到了两组数字(即 123 和 10),但它在“匹配信息”面板中将它们都标记为 "Group 1" .
我的问题是:
1) 我怎样才能 select 只返回第 2 组?
2) 从字符串的某个点开始正则表达式是否更好,比如 '(' 字符?即使阅读了它,我也不确定该怎么做。
只需捕获模式中由任何非数字分隔的两组数字:
>>> import re
>>> s = "123= TEXTINCAPS('Example One',$,$,10.,$);"
>>> m = re.match(r'(\d+)[^\d]+(\d+)', s)
>>> m.group(1)
123
>>> m.group(2)
10
只找到所有个号码然后选择第二个!
所以你的第一个例子:
e1 = "123= TEXTINCAPS('Example One',$,$,10.,$);"
我们可以做到:
re.findall("\d*\.*\d+", e1)
给出:
["123", "10"]
还有:
e2 = "123= TEXTINCAPS('Example Two',$,$,0.2521,$);"
我们得到:
['123', '0.2521']
很明显,如果您只想要第二个索引,只需使用 re.findall(...)[1]
.
如果您的格式始终是函数调用的格式,您可以完全跳过正则表达式并以逗号分隔:
line = "123= TEXTINCAPS('Example One',$,$,10.,$);"
print(line.split(',')[-2])
(注意:首选负索引,因为前导引号字符串中可能有一个或多个逗号。)
You can use Positive Lookbehind (?<=,) :
import re
pattern=r'(?<=,)[0-9.]+'
text="""123= TEXTINCAPS('Example Two',$,$,0.2521,$);
"123= TEXTINCAPS('Example One',$,$,10.,$);"""
match=re.finditer(pattern,text)
for find in match:
print(find.group())
输出:
0.2521
10.
正则表达式解释:
Positive Lookbehind (?<=,)
Assert that the Regex below matches
, matches the character , literally (case sensitive)
Match a single character present in the list below [0-9.]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9
a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
. matches the character . literally (case sensitive)