Matching C integer constants with re

Just just at xs4all.nl
Thu Jun 12 04:49:17 EDT 2003


In article <1xxzofj8.fsf at python.net>,
 Thomas Heller <theller at python.net> wrote:

> 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?

I'm by no means a regex expert, but I think you have to match the longer 
ones first:

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

This seems to fix your script for me.

Just




More information about the Python-list mailing list