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.

Answer from Felix Kling on Stack Overflow
๐ŸŒ
Regex101
regex101.com
regex101: build, test, and debug regex
Online regex tester and debugger with real-time match highlighting, detailed explanations, substitutions, unit tests, benchmarking, and code generation. Supports PCRE2, JavaScript, Python, Go, Java, .NET, Rust, POSIX ERE, and POSIX BRE.
๐ŸŒ
RAKKOTOOLS
en.rakko.tools โ€บ home โ€บ programming โ€บ regular expression checker
Regular Expression Checker | RAKKOTOOLS
A free tool that lets you check and validate on the spot whether a PHP, JavaScript, or Python regular expression correctly matches the target text. It also supports flags such as i, g, m, s, and u, useful for checking pattern behavior, debugging, ...
Discussions

Python Regex vs PHP Regex - Stack Overflow
If you want to use a regex, just verify that it kind of "looks like" an IP address, as with Greg's regex. ... Without further details, I'd guess it's quote escaping of some kind. Both PHP and python's RegEX objects take strings as arguments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 19, 2011
Is there some website which can translate Perl regex to Python regex?

Cygwin comes with a tool called txt2regex. Which is a command line wizard for creating regex and will output it in perl, php, postgres, python, sed, and vim formats. While it's probably available on native linux distros, I don't think it would support conversion of one format to another. Someone who is adventurous could probably write a converter based on the above codebase...

More on reddit.com
๐ŸŒ r/regex
2
3
July 27, 2018
php regular expression convert to python code - Stack Overflow
i have this code written in php, and i want to convert it into python code $title_regex = "/ (.+) /i"; preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER); $ More on stackoverflow.com
๐ŸŒ stackoverflow.com
Regex from Python in PHP - Stack Overflow
I've got the script in Python data = re.sub("[\xF3].[\x00]","\n",data) How to use that regex in PHP? $data = preg_replace("[\xF3].[\x00]", "\n", $data); I tried it but doesn't work. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 6
13

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.

2 of 6
11

They are compatible for the most part. There are some differences, though, apart from the different syntax (/regex/ in PHP vs. re.compile(r"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.

๐ŸŒ
ExtendsClass
extendsclass.com โ€บ regex-tester.html
Online Regex tester and visualizer - Python, PHP, Ruby, JavaScript, Java, MySQL
CyrilEx is an online regex debugger, it allows you to test regular expression in PHP (PCRE), Python, Ruby, JavaScript, Java and MySQL.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_regex.asp
PHP Regular Expressions
Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions.
๐ŸŒ
regex101
regex101.com โ€บ library โ€บ 5VyM27
PHP preg_match
Please complete this security check to continue to regex101.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 40291192 โ€บ regex-from-python-in-php
Regex from Python in PHP - Stack Overflow
I've got the script in Python data = re.sub("[\xF3].[\x00]","\n",data) How to use that regex in PHP? $data = preg_replace("[\xF3].[\x00]", "\n", $data); I tried it but doesn't work.
๐ŸŒ
Regexplanet
regexplanet.com
RegexPlanet
Regular expression (regex/regexp) testing and debugging for Your Browser, Bun, Deno, Go, Java, MySQL, .NET, Node.js, Perl, PHP, PostgreSQL, Python, Ruby, Rust, Swift, tcl, XRegExp.
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ online regex tester and debugger: javascript, python, php, and pcre
r/PHP on Reddit: Online regex tester and debugger: JavaScript, Python, PHP, and PCRE
January 22, 2015 - If youโ€™re looking for a clean and simple online regex tool, regex101 is definitely a popular choice. The breakdown explanations and flavour selection (JS, Python, PHP, PCRE) make debugging much easier, especially when youโ€™re switching between languages.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.7 ...
The third-party regex module, which has an API compatible with the standard library re module, but offers additional functionality and a more thorough Unicode support.
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ unicode modifier
Regular Expressions Tutorial => UNICODE modifier
Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8. In Python 2.x, the re.UNICODE only affects the pattern itself: Make \w, \W, \b, \B, \d, \D, \s and \S dependent on the Unicode character properties database.
๐ŸŒ
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp). Supports JavaScript & PHP/PCRE RegEx.
๐ŸŒ
UI Bakery
uibakery.io โ€บ regex-library โ€บ url-regex-php
URL regex PHP
Example code in Python: // Validate if a string is a valid URL format without protocol $url_validation_regex = "/^[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$/"; echo preg_match($url_validation_regex, 'uibakery.io'); // returns 1 // Extract URL without protocol from a string $extract_url_pattern = "/[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)/"; $string_to_match = 'You can view more details at uibakery.io or just ping via email.'; preg_match_all($extract_url_pattern, $string_to_match, $matches); print_r($matches[0])// matches[0] is ['uibakery.io'] Test it!
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ php tutorials: learn php from scratch โ€บ php regular expressions
PHP Regular Expressions
July 4, 2025 - This Python RegEx tutorial explains what is Regular Expression in Python and how to use it with the help of programming examples: Searching characters in a string are one of the most common tasks while working with strings in any programming language.
๐ŸŒ
Rexegg
rexegg.com โ€บ regex-modifiers.php
What are regex modifiers, and how to turn them on?
block_regex = pcre_compile( "BEGIN .*? END", PCRE_DOTALL, &error, &erroroffset, NULL ); In PHP, the PCRE_DOTALL option is passed via the s flag, which you can add in your regex string after the closing delimiter. For instance, you can use: $block_regex = '~BEGIN .*? END~s'; Python Apart from the (?s) inline modifier, Python has the DOTALL option.