[email protected] wrote:
> I've encountered a problem with my RegEx learning curve -- how to
> escape hash characters # in strings being matched, e.g.:
>
>>>> string = re.escape('123#abc456')
>>>> match = re.match('\d+', string)
>>>> print match
>
> <_sre.SRE_Match object at 0x00A6A800>
>>>> print match.group()
>
> 123
>
> The correct result should be:
>
> 123456
>
> I've tried to escape the hash symbol in the match string without
> result.
>
> Any ideas? Is the answer something I overlooked in my lurching Python
> schooling?
As you're not being clear on what you wanted, I'm just guessing this is
what you wanted:
>>> s = '123#abc456'
>>> re.match('\d+', re.sub('#\D+', '', s)).group()
'123456'
>>> s = '123#this is a comment and is ignored456'
>>> re.match('\d+', re.sub('#\D+', '', s)).group()
'123456'
--
http://mail.python.org/mailman/listinfo/python-list