Skip to content Skip to sidebar Skip to footer

How Can I Use Pcre Regexes From A Python Script?

I would like to use the PCRE library, or something very similar, from Python scripts. These scripts would be for personal use so less portable, fast and concise code would be accep

Solution 1:

Tor, unearthing this old question because it's a good one, and still timely for anyone who wants to move a project from PHP to python.

I too am a big fan of PCRE. On Python, re is quite limited, but the good news is that there's another module called regex that offers many of the wonderful features of PCRE, including:

  1. Atomic Groups
  2. Possessive Quantifiers
  3. Recursion
  4. Branch Reset

and many more, including several that PCRE doesn't offer, such as class subtraction, variable-length lookbehind and fuzzy matching.

If the developers carry the project to fruition, Python will suddenly be equipped with an outstanding regex engine, which will make it more attractive to regex lovers who have been on the fence about switching from PHP.

Read the directions carefully, because the module can be run in two modes, one for compatibility with re, the other with all the cool features. You specify the mode with (?V0) or (?V1)

On Unix, installation with pip is a snap. On Windows, I haven't managed to get the regex module to work with a 64-bit python. I uninstalled my 64-bit installs of 2.7 and 3.3, reinstalled in 32-bit, and installed the regex module on both.

Places to go from here:

  1. download page for the regex module.
  2. A thorough SO question about python regex modules

Post a Comment for "How Can I Use Pcre Regexes From A Python Script?"