简体   繁体   English

将Ruby正则表达式转换为php / perl正则表达式

[英]translate Ruby regex to php/perl regex

I have made this regex, which works fine in Ruby: 我做了这个正则表达式,它在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 . 它用于从文本文档中提取电话号码,但是现在我在PHP中使用了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? 为了使此表达式在PHP中起作用,我需要更改什么?

You have a strange error in your Regexp. 您的Regexp中有一个奇怪的错误。 I think it's partially luck it's working on Rubular: 我认为在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: 现在可以使用PHP正常工作:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM