Matching C integer constants with re

Thomas Heller theller at python.net
Thu Jun 12 04:39:55 EDT 2003


I'm trying to match C integer constants with the re module, but cannot
get it to work correctly.
This is the pattern I use:

 re.compile(r"(0[xX][0-9a-fA-F]+|\d+)([uU]|[lL]|[uU][lL]|[lL][uU])?")

and it fails to match constants with a two character suffix like
these: "123ul" and "123lu", although "123u" and "123l" are found correctly.
Can anyone see what's wrong?

Thanks,

Thomas

PS: Here's the test script:

-----
import re

text = "123 0X123 123U 123L 123UL 0xabc2fUL 0xabc2fLU"

pattern = re.compile(r"(0[xX][0-9a-fA-F]+|\d+)([uU]|[lL]|[uU][lL]|[lL][uU])?")

for token in text.split():
    match = pattern.match(token)
    if not match:
        continue
    s, e = match.start(), match.end()
    print token[s:e]
-----
and the output:

123
0X123
123U
123L
123U
0xabc2fU
0xabc2fL




More information about the Python-list mailing list