开发者

Converting RE code from PHP to Python

开发者 https://www.devze.com 2022-12-12 00:00 出处:网络
I have done a simple program in PHP, now need to convert this into Python: $string=\"Google 1600 Amphitheatre Parkway Mountain View, CA 94043 phone\";

I have done a simple program in PHP, now need to convert this into Python:

$string="Google 1600 Amphitheatre Parkway Mountain View, CA 94043 phone";
preg_match_all('/[0-9]+.{10,25}[^0-9]*[0-9]{开发者_开发技巧5,6}+\s/',$string,$matches);
print_r($matches);


import re
for x in re.findall('[0-9]+.{10,25}[^0-9]*[0-9]{5,6}\s',STRING):print x

will be ok for you?


In python, you must use the "re" module to do that. Unlike in PHP, you don't need to place delimitors, so strip the "/" you have at the begining and the end of the pattern.

The Python idiom would then be :

import re
string="Input values"
for match in re.findall('[0-9]+.{10,25}[^0-9]*[0-9]{5,6}\s',string) : 
    print match

We rarely use some pretty print tools such as "print_r" in Python because most of the time, iterators and str() do the trick. So here, a simple for loop will do the job.


import re
string = "Input values"
match = re.match('/[0-9]+.{10,25}[^0-9]*[0-9]{5,6}\s/', s)
print match

this should be what you're looking for, if the RegEx is right.

0

精彩评论

暂无评论...
验证码 换一张
取 消