%d not working in re at Python 2.7?

Vacu vacu001 at gmail.com
Mon May 14 19:56:03 EDT 2012


Thanks Tim, it is my mis-understanding of usage of %d in Python. After
reading it carefully, it should be used in re.scan.
The reason I made this dump mistake is because I got a script from our
expert and I am totally new on Python, before reading your
email, I hadn't a doubt it is wrong usage and didn't examine document
carefully. I apologize for you time spent on it. -Vacu

On Mon, May 14, 2012 at 4:20 PM, Tim Chase <python.list at tim.thechases.com>wrote:

> On 05/11/12 13:58, vacu 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'
> >
> > Do you have any idea what's problem here?
>
> Because the regexp module doesn't support using "%d" in this way?
> I'd be curious is you can point to Python documentation to the contrary.
>
> You know what the problem is, you didn't spell it r"\d+" so I'm not
> sure why you're asking.  I've tested as far back as Python2.3 and
> "%d" hasn't worked like you seem to expect it to in any of those
> versions.
>
> As an aside, use raw strings (as in r"\d+" with the leading "r")
> instead of regular strings to ensure escaping applies as you expect
> it to.
>
> -tkc
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120514/708a2b16/attachment.html>


More information about the Python-list mailing list