r' question
Dick Moores
rdm at rcblue.com
Sun May 18 01:49:03 EDT 2008
At 10:17 PM 5/17/2008, Gabriel Genellina wrote:
>En Sat, 17 May 2008 23:37:16 -0300, Dick Moores <rdm at rcblue.com> escribió:
>
> > I have a text file of phone numbers, which I'd like to search with a regex.
> >
> > fstr = "\sjoe\s"
> > regex = "^.*" + fstr + ".*$"
> >
> > fstr = "\sjoe\s"
> > regex = "r'^.*" + fstr + ".*$'"
>
>The r"..." is a signal to the parser - meaning
>"don't interpret the escape characters here".
>Note that the r is OUTSIDE the quotes. In your
>example, the escape characters are in fstr, so
>it should be written as r"\sjoe\s"
However, (please refer back to my original post)
I want to keep the fstr, ultimately to be the
string entered by the user who knows a bit about
regex, but not how to use r' ' . Or
alternatively, not assume any knowledge of regex,
but build in some options, such as ignoring/not
ignoring case, searching on just a string, or on
a word. So I want to know how to build the user's
fstr into regex. I apologize for not making this clear.
>Now, if you want "the lines that contain the
>word joe surrounded by space", just use:
>
>import re
>regex = r"\sjoe\s"
>p = re.compile(regex, re.I)
>f = open('phone.txt', 'r')
>for line in f:
> m = p.search(line)
> if m:
> print m.group()
>f.close()
>
>A file is its own line iterator (no need for
>readlines). And since you're iterating over
>lines, the regex doesn't have to test for it ^ and $.
Yes, that works. Thanks.
Dick Moores
More information about the Python-list
mailing list