tags:

views:

134

answers:

4

In Perl I would do something like this for taking different fields in a regexp, separating different fields by () and getting them using $

foreach $line (@lines)
{
 $line =~ m/(.*?):([^-]*)-(.*)/;
  $field_1 = $1
  $field_2 = $2
  $field_3 = $3
}

How could I do something like this in Python?

+7  A: 

Python supports regular expressions with the re module. The re.search() method returns a MatchObject which has methods like group() which you can use to retrieve the "capturing group" information.

For example:

m = re.search(r'(.*?):([^-]*)-(.*)', line)
field_1 = m.group(1)
field_2 = m.group(2)
field_3 = m.group(3)
Adam Batkin
+9  A: 

In Perl, you'd be much better off using an array than suffixing a bunch of scalars with numbers. E.g.

foreach my $line ( @lines ) { 
    my @matches = ( $line =~ m/(.*?):([^-]*)-(.*)/ );
    ...
}

In Python, the re module returns a match object containing the capture-group information. So you could write:

match = re.search( '(.*?):([^-]*)-(.*)', line )

Then your matches would be available in match.group(1), match.group(2), etc.

friedo
+4  A: 

And don't forget that in Python, TIMTOWTDI ;)

import re
p = re.compile(r'(\d+)\.(\d+)')
num_parts = p.findall('11.22   333.444') # List of tuples.
print num_parts                          # [('11', '22'), ('333', '444')]
FM
+7  A: 

"Canonical" Python translation of your snippet...:

import re

myre = re.compile(r'(.*?):([^-]*)-(.*)')
for line in lines:
    mo = myre.search(line)
    field_1, field_2, field_3 = mo.groups()

Importing re is a must (imports are normally done at the top of a module, but that's not mandatory). Precompiling the RE is optional (if you use the re.search function instead, it will compile your pattern on the fly) but recommended (so you don't rely on the module cache of compiled RE objects for your performance, and also in order to have a RE object and call its methods, which is more common in Python).

You can use either the match method (which always tries matching from the start, whether or not your pattern starts with '^') or the search method (which tries matching anywhere); with your given pattern they should be equivalent (but I'm not 100% sure).

The .groups() method returns all matching groups so you can assign them all in one gulp (using a list in Python, just like using an array in Perl, would probably be more normal, but since you chose to use scalars in Perl you can do the equivalent in Python too).

This will fail with an exception if any line does not match the RE, which is fine if you know they all do match (I'm not sure what's the behavior of your Perl but I think it would "reuse" the previous matching line's values instead, which is peculiar... unless, again you know all lines match;-). If you want to just skip non-matching lines, change the last statement to the following two:

    if mo:
        field_1, field_2, field_3 = mo.groups()
Alex Martelli