rejecting newlines with re.match
Vlastimil Brom
vlastimil.brom at gmail.com
Thu Nov 27 04:41:15 EST 2008
2008/11/27 r0g <aioe.org at technicalbloke.com>
> Hi,
>
> I want to use a regex to match a string "poo" but not "poo\n" or
> "poo"+chr(13) or "poo"+chr(10) or "poo"+chr(10)+chr(13)
>
> ...
> Thanks,
>
>
> Roger.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hi,
if I understand correctly, that you want to exclude matches of strings
followed by certain characters, you may use the negative lookahead assertion
(you can consult the details in the references you mentioned); cf.:
>>> re.findall(r'(?ms)\b\w+\b', 'to match a string poo1 but not poo2\n
poo3\rpoo4\npoo5\n\rqwer')
['to', 'match', 'a', 'string', 'poo1', 'but', 'not', 'poo2', 'poo3', 'poo4',
'poo5', 'qwer']
>>> re.findall(r'(?ms)\b\w+\b(?![\r\n])', 'to match a string poo1 but not
poo2\n poo3\rpoo4\npoo5\n\rqwer')
['to', 'match', 'a', 'string', 'poo1', 'but', 'not', 'qwer']
>>>
The pattern is a bit modified - here all "words" should be matched (in the
second example excluding the ones followed by \r or \n).
hth
Vlasta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081127/714e2428/attachment-0001.html>
More information about the Python-list
mailing list