trying to match a string
ojeeves at gmail.com
Mon Jul 21 11:23:43 CEST 2008
On Jul 19, 3:04 am, Andrew Freeman <alif... at gmail.com> wrote:
> let me revise it please:
> To show if valid:
> if re.search(r'^[LRM]*$', 'LM'):
> print 'Valid'
Fine, this works, although match instead of search blah blah blah as
has already been mentioned. I still think searching for one invalid
character is more elegant then trying to match the entire string, but
that's just personal preference, I guess.
> To show if invalid,
> if re.search(r'^[^LRM]*$', '0'):
> print 'Inalid'
No. This is wrong. This only matches strings that consist entirely of
characters that are not L, R or M:
>>> import re
>>> if re.search(r'^[^LRM]*$', 'ZZZLZZZ'):
... print "Invalid"
This doesn't print "Invalid" because there is one non-invalid
character there, which is clearly not what the OP wanted.
More information about the Python-list