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?
-
1what's wrong with import re ?PW.– PW.2013-06-05 12:56:48 +00:00Commented Jun 5, 2013 at 12:56
-
@PW. it's not PCRE? Or is your question why PCRE instead of just Python's own regex flavor?Martin Ender– Martin Ender2013-06-05 12:58:52 +00:00Commented Jun 5, 2013 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.– PW.2013-06-05 13:06:02 +00:00Commented Jun 5, 2013 at 13:06
-
5Python's built-in regexes are pretty much PCRE. Is there a specific feature they lack that you want?user1919238– user19192382013-06-05 13:12:47 +00:00Commented Jun 5, 2013 at 13:12
-
It's more that I just don't want to worry about the differences, however small. (Might be too anal.)user912475– user9124752013-06-05 21:08:35 +00:00Commented Jun 5, 2013 at 21:08
1 Answer
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:
- Atomic Groups
- Possessive Quantifiers
- Recursion
- 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: