a newbie regex question
Dotan Cohen
dotancohen at gmail.com
Fri Jan 25 08:45:36 EST 2008
On 24/01/2008, Jonathan Gardner <jgardner at jonathangardner.net> wrote:
> On Jan 24, 12:14 pm, Shoryuken <sakradevanamin... at gmail.com> wrote:
> > Given a regular expression pattern, for example, \([A-Z].+[a-z]\),
> >
> > print out all strings that match the pattern in a file
> >
> > Anyone tell me a way to do it? I know it's easy, but i'm completely
> > new to python
> >
> > thanks alot
>
> You may want to read the pages on regular expressions in the online
> documentation: http://www.python.org/doc/2.5/lib/module-re.html
>
> The simple approach works:
>
> import re
>
> # Open the file
> f = file('/your/filename.txt')
>
> # Read the file into a single string.
> contents = f.read()
>
> # Find all matches in the string of the regular expression and
> iterate through them.
> for match in re.finditer(r'\([A-Z].+[a-z]\)', contents):
> # Print what was matched
> print match.group()
Maybe you mean:
for match in re.finditer(r'\([A-Z].+[a-z])\', contents):
Note the last backslash was in the wrong place.
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
More information about the Python-list
mailing list