Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composable Regexp in Python

Tags:

python

regex

Often, I would like to build up complex regexps from simpler ones. The only way I'm currently aware of of doing this is through string operations, e.g.:

Year = r'[12]\d{3}'
Month = r'Jan|Feb|Mar'
Day = r'\d{2}'
HourMins = r'\d{2}:\d{2}'

Date = r'%s %s, %s, %s' % (Month, Day, Year, HourMins)
DateR = re.compile(Date)

Is anybody aware of a different method or a more systematic approach (maybe a module) in Python to have composable regexps? I'd rather compile each regexp individually (e.g. for using individual compile options), but then there doesn't seem to be a way of composing them anymore!?

like image 243
ThomasH Avatar asked Jul 20 '09 21:07

ThomasH


People also ask

What is W+ in Python regex?

\w+ matches one or more word characters (same as [a-zA-Z0-9_]+ ). \. matches the dot (.) character. We need to use \. to represent . as . has special meaning in regex.

Can you do regex in Python?

Since then, regexes have appeared in many programming languages, editors, and other tools as a means of determining whether a string matches a specified pattern. Python, Java, and Perl all support regex functionality, as do most Unix tools and many text editors.

Does Python replace support regex?

Regex can be used to perform various tasks in Python. It is used to do a search and replace operations, replace patterns in text, check if a string contains the specific pattern.


1 Answers

You can use Python's formatting syntax for this:

types = {
    "year":           r'[12]\d{3}',
    "month":        r'(Jan|Feb|Mar)',
    "day":            r'\d{2}',
    "hourmins":    r'\d{2}:\d{2}',
}
import re
Date = r'%(month)s %(day)s, %(year)s, %(hourmins)s' % types
DateR = re.compile(Date)

(Note the added grouping around Jan|Feb|Mar.)

like image 77
Glenn Maynard Avatar answered Oct 12 '22 23:10

Glenn Maynard