Matching C integer constants with re

Thomas Heller theller at python.net
Thu Jun 12 04:57:32 EDT 2003


Just <just at xs4all.nl> writes:

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

Yup.

Thanks, Thomas




More information about the Python-list mailing list