%d not working in re at Python 2.7?

Benjamin Kaplan benjamin.kaplan at case.edu
Mon May 14 19:19:19 EDT 2012


On May 14, 2012 7:06 PM, "vacu" <vacu001 at gmail.com> wrote:
>
> I am frustrated to see %d not working in my Python 2.7 re.search, like
> this example:
>
> >>> (re.search('%d', "asdfdsf78asdfdf")).group(0)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: 'NoneType' object has no attribute 'group'
>
>
> \d works fine:
>
> >>> (re.search('\d+', "asdfdsf78asdfdf")).group(0)
> '78'
>
>
> And google search ignores % in their search, so I failed to find
> answer from Python mailing list or web,
> Do you have any idea what's problem here?
>
> Thanks a head
> Vacu
> --

There's no problem at all. This is re.search, not scanf. They aren't
supposed to behave the same. In fact, the docs specifically describe how to
simulate scanf using re because python doesn't have a scanf function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120514/aaf45865/attachment.html>


More information about the Python-list mailing list