3

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 acceptable. How can I tell python to send a string and a regex to pcre and get the result returned?

HamZa
  • 14,671
  • 11
  • 54
  • 75
  • 1
    what's wrong with import re ? – PW. Jun 05 '13 at 12:56
  • @PW. it's not PCRE? Or is your question why PCRE instead of just Python's own regex flavor? – Martin Ender Jun 05 '13 at 12:58
  • @m.buettner, no it's not PCRE, it's 'Secret Lab's re engine' (python 2) , and yes why not using standard Python lib in this case ? – PW. Jun 05 '13 at 13:06
  • 5
    Python's built-in regexes are pretty much PCRE. Is there a specific feature they lack that you want? –  Jun 05 '13 at 13:12
  • It's more that I just don't want to worry about the differences, however small. (Might be too anal.) –  Jun 05 '13 at 21:08

1 Answers1

4

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
Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • 1
    Well, it's just the one developer. It's been his pet project for years, and he's been trying to convince the rest of the core devs to put it in the standard library all this time. Probably the best thing folks can do to nudge things in that direction is to download it and use it in earnest. (This shows the demand, as well as helps find bugs and usability issues so that the package can be made as robust as possible.) Also, [here's another SO question](http://stackoverflow.com/questions/7063420/perl-compatible-regular-expression-pcre-in-python) about PCRE with Python. – John Y Apr 22 '14 at 19:42