Skip navigation.

Regex Capture Groups In Python and Perl

Regex Capture Groups In Python and Perl

I am a Python programmer and ex-Perl hacker.

Regular Expressions are possibly the quintessential feature of Perl and are directly part of the language syntax.

Rather than being part of the syntax, Python's Regular expressions are available via the 're' module. For some reason, I had some trouble figuring out matching groups when I first started using Python's Regular Expressions.

He are examples of extracting capture groups in both Perl and Python.

Lets say we have a string containing a date: '11/14/2007', and we want to capture only the year from this string.

A regex to match this format might be something like this:

[0-9]{2}/[0-9]{2}/[0-9]{4} 

We can then put parenthesis around the piece we want to extract (the 4-digit year) to denote a capture group.

So now our regex would look like this:

[0-9]{2}/[0-9]{2}/([0-9]{4}) 


Perl Example:

$foo = '11/14/2007'; if ($foo =~ m^[0-9]{2}/[0-9]{2}/([0-9]{4})^)
{ print $1; } 

output:

2007 

* Note the string we captured ended up in the special variable $1


Python Example:

import re foo = '11/14/2007' match = re.search('[0-9]{2}/[0-9]{2}/([0-9]{4})',
foo) if match: print match.group(1) 

output:

2007 

* Note the string we captured ended up in a match object, which can be accessed with the 'group()' method.