I am trying to make a regular expression that matches a line when it has a number followed by a ok word.
Eg:
10 ok
But if there is a number and nok after the ok word, then it should not match. Eg:
10 ok 2 nok
I am using the following regular expression to achieve this:
[0-9]+\s+ok\s+(?!([0-9]+\s+nok))
I am using the 4th answer from Which regular expression operator means 'Don't' match this character? to generate a not
functionality in my regex.
Here is my code:
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)
But my pattern still matches with a line that contains nok