regexp matching end of line or comma

Sol Toure sol2ray at gmail.com
Thu Nov 25 11:02:49 EST 2010


Try this: '(?P<something>\S+)(,|$)'

On Thu, Nov 25, 2010 at 9:40 AM, Jean-Michel Pichavant <
jeanmichel at sequans.com> wrote:

> Hy guys,
>
> I'm struggling matching patterns ending with a comma ',' or an end of line
> '$'.
>
> import re
>
> ex1 = 'sumthin,'
> ex2 = 'sumthin'
> m1 = re.match('(?P<something>\S+),', ex1)
> m2 = re.match('(?P<something>\S+)$', ex2)
> m3 = re.match('(?P<something>\S+)[,$]', ex1)
> m4 = re.match('(?P<something>\S+)[,$]', ex2)
>
> print m1, m2
> print m3
> print m4
>
> <_sre.SRE_Match object at 0x8834de0> <_sre.SRE_Match object at 0x8834e20>
> <_sre.SRE_Match object at 0x8834e60>
> None
>
> My problem is that m4 is None while I'd like it to match ex2.
>
> Any clue ?
>
> JM
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.afroblend.com
African news as it happens.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101125/c77d310d/attachment.html>


More information about the Python-list mailing list