[Tutor] Regexp with multiple patterns in Python
ingarse at broadpark.no
Sun Aug 14 18:16:32 CEST 2005
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:
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) -
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!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor