Help with regular expressions

Tony Meyer ta-meyer at ihug.co.nz
Tue Aug 26 05:17:56 EDT 2003


> Any other suggestions?

If you're going to be maintaining it (and if you can't find someone to fix
it, I presume you are), then you might as well bite the bullet and learn the
basics of regular expressions.  I put this off for ages, but it didn't take
all that long once I finally did, and I really regret that I didn't earlier.

This page is great as a tutorial and reference:
<http://www.amk.ca/python/howto/regex/>

> The question I have, is how do I go about fixing this.  What I want is to
> test to see if the line does or doesn't contain an "at", and if not,
> change it to contain an "at".  I'm just not sure how to code the RE in
> python to do this. 

For this particular case, you can just change the '\sat\s' (in the last
line) to '(?:\sat\s)?'.  This makes the at a group (the parentheses), which
isn't captured (the ?:) and which is optional (the ? at the end) - the \s
means that there is whitespace around the 'at'.  There are no doubt other
(probably better!) ways, but this should do what you ask.

=Tony Meyer






More information about the Python-list mailing list