tags:

views:

94

answers:

3

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);
A: 
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.

TiPoK
+1  A: 
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?

S.Mark
+1, but some explanations would be great.
e-satis
Thanks for pointing out, since you already did that, he would understand that. I am very new to stackoverflow actually, anyway cheers.
S.Mark
A: 

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.

e-satis
Thanks a lot its working fine
Naresh
LOl, it's not a "textual" agreement, you must click on the "accept" bouton.
e-satis