I have some regular expressions written for the Microsoft's .net regex format and I want to use them in a python program on a Linux machine. Is there a compatibly library or a method of covering them? If I have to do it by hand, do you know of a cheat sheet or a guide?
4 Answers
Most of the features in .NET regexes are available in Python; however there are a few things missing:
- Python doesn't support variable repetition inside lookbehinds, only fixed-length.
- Python uses a different syntax for named captures.
- Python matches Unicode characters differently.
- Python doesn't have atomic groups
(?>...)
or possessive quantifiers++
,*+
,?+
. - In Python, use
r"..."
raw strings for regular expressions, or you'll trip up on backslashes.
A comprehensive list of differences can be found here.
And if you need to do regex conversions often, RegexBuddy can do it for you (as long as you're not using features the destination regex engine doesn't have, of course).
-
Wait i have a copy of RegexBuddy. What is the feature called?rook– rook11/05/2010 18:07:13Commented Nov 5, 2010 at 18:07
-
@Rook: Copy the string from your C# code, open RegexBuddy, click the "Paste" button, "Paste from..." and "Paste Regex from a C# string", then go to the "Use" tab and choose Python.Tim Pietzcker– Tim Pietzcker11/05/2010 18:53:12Commented Nov 5, 2010 at 18:53
The docs for the re
module cover Python regex syntax. FWIW, the syntax is very similar, except for the substitutions.
In terms of implementation I think the CLR and Python regex engines are very close, more so than either of them to other languages like Perl or the PCRE. For example, I've used Expresso to create and test complicated regular expressions that I've later adapted to Python code.
That said, I don't think I've ever seen an actual converter, but since they're so similar it should be relatively easy to convert them manually (again, more so than if we were talking about other implementations).
This page has good comparisons between the different engines, although it focuses on capabilities rather than similarity of syntax.
Are you sure there's an incompatibility? At least for the basic to middle-level stuff in regexes, all the implementations pretty much agree on the syntax, except for maybe whether parens should be escaped or not.
-
Yeah i'm 150% sure these regular expressions are throwing an exception when it try and use them.rook– rook11/05/2010 17:52:28Commented Nov 5, 2010 at 17:52