Perl like regex in Python


Question

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?

1
10
4/14/2010 1:13:14 PM

"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()
18
4/14/2010 2:21:32 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon