我正在尝试创建一个正则表达式,当它有一个数字后跟一个ok字时匹配一行。
例如:
10 ok
但是如果ok字之后有一个数字和nok,那么它就不匹配了。 例如:
10 ok 2 nok
我使用以下正则表达式来实现此目的:
[0-9]+\s+ok\s+(?!([0-9]+\s+nok))
我正在使用Which regular expression operator means 'Don't' match this character?的第4个回答在我的正则表达式中生成not
功能。
这是我的代码:
import re
prog=re.compile('[0-9]+\s+ok\s+(?!([0-9]+\s+nok))')
result=prog.search('108601 ABC_kill 11 ok 3 nok 95m 25_KLPO casdas5 dus41 fdd tm500 sdfsd1010_1014 2m 2016-02-11 02:30:50 2016-02-11 08:53:59')
print (result)
但我的模式仍然与包含nok
答案 0 :(得分:2)
答案 1 :(得分:1)
s='10 ok 2 nok'
# ^---- two spaces here
re.search(r'[0-9]+\s+ok\s+(?![0-9]+\s+nok)', s)
will succeed. Let's see what happens:
[0-9]+\s+ok\s+
matches '10 ok '
(with the two spaces), but after (?![0-9]+\s+nok)
fails.
At this point, the regex engine uses the backtracking mechanism and \s+
gives back a character (the last space), then [0-9]+\s+ok\s+
matches '10 ok '
(only one space) and (?![0-9]+\s+nok)
succeeds with ' 2 nok'
To avoid the backtracking, you can emulate an atomic group (?>...)
(that forbids backtracking once closed) with (?=(...))\1
(a lookaround is naturally atomic):
(?=([0-9]+\s+ok\s+))\1(?![0-9]+\s+nok)