[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

a newbie regex question

Shoryuken

1/24/2008 8:14:00 PM

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
2 Answers

Jonathan Gardner

1/24/2008 9:41:00 PM

0

On Jan 24, 12:14 pm, Shoryuken <sakradevanamin...@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/modu...

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()

Max Erickson

1/25/2008 11:58:00 PM

0

"Dotan Cohen" <dotancohen@gmail.com> wrote:
> Maybe you mean:
> for match in re.finditer(r'\([A-Z].+[a-z])\', contents):
> Note the last backslash was in the wrong place.

The location of the backslash in the orignal reply is correct, it is
there to escape the closing paren, which is a special character:

>>> import re
>>> s='Abcd\nabc (Ab), (ab)'
>>> re.findall(r'\([A-Z].+[a-z]\)', s)
['(Ab), (ab)']

Putting the backslash at the end of the string like you indicated
results in a syntax error, as it escapes the closing single quote of
the raw string literal:

>>> re.findall(r'\([A-Z].+[a-z])\', s)

SyntaxError: EOL while scanning single-quoted string
>>>


max