regex - Why does my regular expression in Python not capture integers? -
i using regular expression find sequences of numbers aren't refixed 0x
.
eg.
0 50505 20201 0012
my regular expression (?:[^(0x)]\d+)
, (in head) translates match sequence of digits not started 0x
. doesn't work - assumption incorrectly making?
[^(0x)]
in regular expression match character not (
, 0
, x
, )
.
use negative lookbehind:
>>> re.findall(r'(?<!0x)\d+\b', '0 0x111 50505 20201 0012') ['0', '11', '50505', '20201', '0012']
from http://docs.python.org/2/library/re.html
(?<!...)
matches if current position in string not preceded match .... called negative lookbehind assertion. similar positive lookbehind assertions, contained pattern must match strings of fixed length. patterns start negative lookbehind assertions may match @ beginning of string being searched.
update
use following regular expression:
>>> re.findall(r'\b\d+\b', '0 0x111 50505 20201 0012') ['0', '50505', '20201', '0012']
Comments
Post a Comment