trying to match a string

John Machin sjmachin at lexicon.net
Fri Jul 18 07:10:44 EDT 2008


On Jul 18, 9:05 pm, oj <ojee... at gmail.com> wrote:
> On Jul 18, 11:33 am, arnimavidyar... at gmail.com wrote:
>
>
>
> > Hi,
>
> > Hi,
>
> > I am taking a string as an input from the user and it should only
> > contain the chars:L , M or R
>
> > I tried the folllowing in kodos but they are still not perfect:
>
> > [^A-K,^N-Q,^S-Z,^0-9]
> > [L][M][R]
> > [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
>
> > For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
>
> > regards,
> > SZ
>
> > The string may or may not have all the three chars.
>
> With regular expressions, [^LRM] matches a character that isn't L, R
> or M. So:
>
> import re
>
> var = "LRLRLRLNR"
>
> if re.search(r'[^LRM]', var):
>     print "Invalid"

Fails if var refers to the empty string.



More information about the Python-list mailing list