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 OverflowPython Regex vs PHP Regex - Stack Overflow
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.comphp regular expression convert to python code - Stack Overflow
Regex from Python in PHP - Stack Overflow
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.
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):
- PCRE supports
\Q...Eto escape metacharacters, Python doesn't. - PCRE supports
\cA-\cZcontrol character matching, Python doesn't. - Hyphen in
[\d-z]or[a-\d]is a literal in PHP, not in Python. - PCRE supports
\z(end-of-string), Python doesn't, only\Z(end-of-string before optional final linefeed). \bwill match word boundaries only around ASCII characters in PCRE, in Python it can match locale-dependently if the option is set.- You can refer to
\1etc. backreferences ahead of their capturing parentheses in PCRE, you can't in Python. - You can't turn off mode modifiers within the regex (
(?-s)etc.) in Python. - You don't get atomic grouping
(?>...)or possessive quantifiers (.++) in Python, only in PCRE. - Lookbehind can be finite-length in PCRE, must be fixed-length in Python.
- There is no
\Gpattern (location of previous match). - No conditional matching in Python, only in PCRE:
(?(?=regex)then|else). - No
\x1234for Unicode code points matching in Python. Nop{L}and other Unicode property matching, either. In PHP, it depends how it's configured/compiled. - 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.
It works for me. You must be doing something wrong.
>>> re.match(r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', '127.255.0.0').groups()
('127', '255', '0', '0')
Don't forget to escape the regex using raw strings: r'regex_here' as stated in the Regex Howto
I would suggest that using a regex for decimal range validation is not necessarily the correct answer for this problem. This is far more readable:
def valid_ip(s):
m = re.match(r"(\d+)\.(\d+)\.(\d+)\.(\d+)$", s)
if m is None:
return False
parts = [int(m.group(1+x)) for x in range(4)]
if max(parts) > 255:
return False
return True
Is there some website which can translate between regex of different flavors, for example from Perl regex to Python regex? Thanks.
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...
I'm not sure of any software that would do that, however, regex101.com allows you to switch languages.
You can write a PERL regex by choosing pcre (php) from the FLAVOR menu and then switch to Python by selecting python from the same list.
If it doesn't work with python, you'll be able to tell and can then start removing parts of the regex until it works and start from there.
Here is a demo showing how the regex works in PERL, but when you switch it to python, it doesn't match. (Because python doesn't support the horizontal whitespace character \h.)
https://regex101.com/r/1lCSHV/1
So, you could change the \h to \s and it will now work.
the regular expression probably did not find anything.
try this : also remove the /Ui at the end
import re
out=Data #web site html page ..
title_regex = "/<title>(.+)<\/title>/i" #no need for this .. un used
if m is not None: # NEW <----------------
m = re.search("<title>(.+)<\/title>", out)
print "title",m.group(1)
#for pics i have tried this but it give me error ..
pics = re.match(r"<img[^>]*src=\"|\'[\"|\']", out)
if pics is not None: # NEW <----------------
print "grop",pics.group(1)
for you 2nd question try this
for filename in pics.groups():
print filename
Working version .. display all images from a given web site using tag IMG src > code:
import re
import urllib
print "Start"
url="http://www.deviantart.com"
data=urllib.urlopen(url)
out=data.read()
print
title_regex = "/<title>(.+)<\/title>/i"
m = re.search("<title>(.+)<\/title>", out)
print "first",m
print "grop",m.group(1)
title_regex = "/<title>(.+)<\/title>/i"
pics = re.compile(r"<IMG[^>]*src=([^>]*[^/])")#Change IMG tag
allpics=pics.findall(out)
print "found",pics
for mypic in allpics:
print "< IMG src=",mypic
thanks all
The reason is that PHP expects delimiters around its regexes, so it treats the first and second slash as delimiters and tries to parse what follows as modifiers.
Surround your regex with new delimiters and try again (I also removed some unnecessary backslashes):
'%/\*\*([\w\n()\[\].*\'"#|,@{}_<>=:/ -]+?)\*/%'
'%(?:\* ([\w\d(),.\'"\:#|/ -]+)|(?<= @)(\w+)(?: (.+))?)%'
Hint: Use RegexBuddy to do these things. It will take a regex written in language A and convert it to language B for you.
PCRE (including preg_match_all) requires a pattern boundary. You need to wrap the entire pattern in /, @, #, %, or many other possible options. I suggest % as it doesn't look like you are using it in either pattern, that is:
%(?:\* ([\w\d\(\),\.\'\"\-\:#|/ ]+)|(?<= @)(\w+)(?: (.+))?)%