Regex in if statement.
Benjamin Kaplan
benjamin.kaplan at case.edu
Sun Mar 20 21:13:50 EDT 2011
On Sun, Mar 20, 2011 at 8:46 PM, Ken D'Ambrosio <ken at jots.org> wrote:
> Hey, all -- I know how to match and return stuff from a regex, but I'd
> like to do an if, something like (from Perl, sorry):
>
> if (/MatchTextHere/){DoSomething();}
>
> How do I accomplish this in Python?
>
> Thanks!
>
> -Ken
>
Python doesn't have regular expressions as a language feature. It's
just a module in the standard library.
import re
if re.search(pattern, string) :
Stuff
However, regular expressions aren't used as much in Python as they are
in Perl. Regular expressions are overkill for a lot of applications.
If you're just checking to see if a string contains some text, use "if
substring in string". If you're checking to see if some text is at the
start of the string, do string.startswith(substring).
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list