简体   繁体   English

为什么这个正则表达式不能在python中工作? 它有效且在RegExr上正常工作

[英]Why isn't this regex working in python? It's valid and works fine on RegExr

I have this regex with I'm using to split a string into tokens I can process later. 我有这个正则表达式,用于将字符串拆分为以后可以处理的标记。 I made it using RegExr (online tool) and it works fine there, but I can't get it to work in python. 我使用RegExr(在线工具)制作了它,并且在那儿工作正常,但是我无法在python中工作。 I'm using this now to print the matches. 我现在正在使用它来打印比赛。

sunit = "A^2.1e2 m/s -180.2"
pat = "[A-Za-z]+(\^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)?|\/|\*|[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?"
Terms = re.findall(pat,sunit)
print Terms

The regex should be matching: 正则表达式应匹配:

A^2.1e2
m
/
s
-180.2

Solved it. 解决了。 Using 're.finditer()' instead of 're.findall()' seems to work. 使用“ re.finditer()”代替“ re.findall()”似乎可行。 Here is what I came up with now. 这是我现在想出的。 Sorry for bothering you, I should have been able solved this a long time ago. 很抱歉打扰您,我早就应该可以解决这个问题的。

sunit = r"A^2.1e2 m/s -180.2"
pat = r"[A-Za-z]+(\^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)?|\/|\*|[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?"
Terms = re.finditer(pat,sunit)
for m in Terms:
    print m.group(0)

You have group captures (...) in your regex. 您的正则表达式中有组捕获(...) If you do not wish to capture the groups, then just remove the brackets from there. 如果您不想捕获这些组,则只需从中删除括号即可。 Or use ?: inside the bracket to ignore it, example (?:...) . 或在括号内使用?:忽略它,例如(?:...) I have changed your regex and it will work with re.findall 我已经更改了您的正则表达式,它将与re.findall一起re.findall

pat = "[A-Za-z]+(?:\^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)?|\/|\*|[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
                 ^^                       ^^                                               ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM