views:

98

answers:

6

hi there,

I found this post: http://stackoverflow.com/questions/118143/python-regex-vs-php-regex but I actually did not get if Python's REGEX syntax matches PHP's REGEX syntax.

I started to convert some of my old PHP code to python (due to g's appengine etc.), and now I would like to know whether the regex is 100% convertable, by simple copy & paste.

regards,

+1  A: 

I believe they're at least mostly compatible, i.e. > 2/3. There may be some language-specific extensions on both sides, but the core is definitely the same. This assertion is based solely on my (limited) personal experience, so take it with a grain of salt.

Both implementations are based on Perl regexes, if I'm not mistaken.

deemoowoor
+2  A: 

Python uses a syntax similar to the Perl syntax and PHP uses the Perl Compatible Regular Expressions syntax, so it should be nearly the same. Read about the possible differences.

The only real difference is that in PHP, the expression must be enclosed in delimiters.

Felix Kling
+right answer, all others matched well too, but your answer fits most perfectly to the question I had in mind =)Thanks to all others, I ll give +1 for every correct/helping answer/comment
daemonfire300
+1  A: 

Not certain of the right answer, but I found a nice tool that will help with your testing.

http://re.dabase.com/

Cheers!

Michael
A: 

After a very quick research, I found out that the main difference is:

PHP (has delimiters)

/ REGEX / # "/" in front and at the end

Python (has no delimiters)

REGEX # no surrounding by any characters
daemonfire300
As pointed out by Felix, the forward slashes are called delimiters.
Russell Dias
+1  A: 

Regular expression engines which are built into various languages usually have differences even if the general syntax is the same. PHP happens have multiple regular expression engines built in (POSIX and PCRE), so depending on which regular expression functions you are using will depend on how well they'll convert over.

If you mainly used preg_* functions then those should mainly convert over without an issue, however I believe the python implementation of regular expressions lacks some more advanced features which are included in the PHP implementation.

You can read about PHP's regular expressions here and Python's regular expressions here and figure out some more specific stuff.

Good question, but difficult to give a complete answer to as there are a lot of variables.

evolve
+2  A: 

They are compatible for the most part. There are some differences, though, apart from the different syntax (/regex/ in PHP vs. `@"regex" in Python):

  1. PCRE supports \Q...E to escape metacharacters, Python doesn't.
  2. PCRE supports \cA-\cZ control character matching, Python doesn't.
  3. Hyphen in [\d-z] or [a-\d] is a literal in PHP, not in Python.
  4. PCRE supports \z (end-of-string), Python doesn't, only \Z (end-of-string before optional final linefeed).
  5. \b will match word boundaries only around ASCII characters in PCRE, in Python it can match locale-dependently if the option is set.
  6. You can refer to \1 etc. backreferences ahead of their capturing parentheses in PCRE, you can't in Python.
  7. You can't turn off mode modifiers within the regex ((?-s) etc.) in Python.
  8. You don't get atomic grouping (?>...) or possessive quantifiers (.++) in Python, only in PCRE.
  9. Lookbehind can be finite-length in PCRE, must be fixed-length in Python.
  10. There is no \G pattern (location of previous match).
  11. No conditional matching in Python, only in PCRE: (?(?=regex)then|else).
  12. No \x1234 for Unicode code points matching in Python. No p{L} and other Unicode property matching, either. In PHP, it depends how it's configured/compiled.
  13. No [:alpha:] POSIX character classes in Python.

Collected from regular-expressions.info, leaving out some of the more esoteric stuff. But not much.

Moral: Buy RegexBuddy and use it to translate the regexes for you.

Tim Pietzcker