vivekporwal04 vivekporwal04 - 5 months ago 23
PHP Question

translate Ruby regex to php/perl regex

I have made this regex, which works fine in Ruby:

/(1?[[\(\s\.-]]\d{3}\)?[\s\.-]\d{3}[\s\.-]\d{4}(\z[^\d])?)/


It's for extracting phone numbers from text document but now I'm using the same expression in PHP, using
preg_match_all
. Now the same expression gives me an empty array.

I don't understand why this is happening

What do I need to change about this expression to make it work in PHP?

Answer

You have a strange error in your Regexp. I think it's partially luck it's working on Rubular:

(1?[[\(\s\.-]]\d{3}\)?[\s\.-]\d{3}[\s\.-]\d{4}(\z[^\d])?)
   ^         ^

Why are those brackets there two times?

See here for a working version where I just removed those. Works fine with PHP now:

http://www.regex101.com/r/gR6kS3/1

(1?[\(\s\.-]\d{3}\)?[\s\.-]\d{3}[\s\.-]\d{4}(\z[^\d])?)
Comments