%d not working in re at Python 2.7?
Tim Chase
python.list at tim.thechases.com
Mon May 14 19:20:27 EDT 2012
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
More information about the Python-list
mailing list