[Tutor] Regexp with multiple patterns in Python

Kristian Evensen ingarse at broadpark.no
Sun Aug 14 18:16:32 CEST 2005


Hello,
 
I am working on a way to parse a file and wondered if there is a way to
check for multiple patterns. The reason I wonder this is because it
would make everything a lot easier regarding calculations on what words
to use and so on.
 
What I want to do is to check for two patterns to make sure all
occurrences of pattern1 and pattern2 come in the same order as they do
in the file I parse. It it contains a number of computer-games I would
like the output to look something like this:

PC, Battlefield, Battlefield2
PS2, Battlefield 2: Modern Combat.
 
The file is constructed somewhat similar to this:
PC
            Battlefield, Battfiled2
PS2
            Battlefield 2: Modern Combat
 
Using the following expression (and re.findall) I get somewhat closer:
pattern8 = re.compile(r'search.asp\?title=battlefield.*?><.*?>(PCCD|XBOX
360|XBOX|PLAYSTATION PSP|PLAYSTATION 2) -
TITLE<|game.asp\?id=(\d+).*?><.*?><.*?>(.*?)<')
 
The output is:
[('PCCD', '', ''), ('PLAYSTATION 2', '', ''), ('XBOX', '', ''), ('XBOX
360', '', ''), ('PLAYSTATION PSP', '', ''), ('', '4262', ' Battlefield
2: Modern Combat')]
 
I get the last game mentioned in the file, but none of the others.
 
Anybody know how to solve this?
 
Thanks in advance for any help and have a great day!
 
-Kristian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050814/eaa0f212/attachment.htm


More information about the Tutor mailing list