[英]Translate regular expression
I would like to translate this JS regular expression 我想翻译这个JS正则表达式
^((8|\\+7)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?[\\d\\- ]{7,10}$
to use in grep. 在grep中使用。 All phone numbers bellow match it: 以下所有电话号码均与其匹配:
+79261234567
89261234567
79261234567
+7 926 123 45 67
8(926)123-45-67
123-45-67
9261234567
79261234567
(495)1234567
(495) 123 45 67
89261234567
8-926-123-45-67
8 927 1234 234
8 927 12 12 888
8 927 12 555 12
8 927 123 8 123
I've tried a lot of variants but no one works for me. 我尝试了很多变体,但没有人适合我。
I haven't tested your pattern, although a few things to consider are: 我没有测试您的模式,尽管需要考虑以下几点:
[\\- ]
should instead be [- ]
or [ -]
[\\- ]
应该改为[- ]
或[ -]
(in a character class
[]
escaping dash is unnecessary if it's first or last). (在字符类[]
如果是第一个或最后一个,则不需要转义破折号)。
[\\d]
will probably fail so either use [0-9]
or [:digit:]
[\\d]
可能会失败,因此请使用[0-9]
或[:digit:]
So this leaves you with: 因此,这使您拥有:
grep -E "^((8|\+7)[ -]?)?(\(?[0-9]{3}\)?[ -]?)?[0-9 -]{7,10}$" file
or egrep
: 或egrep
:
egrep "^((8|\+7)[ -]?)?(\(?[0-9]{3}\)?[ -]?)?[0-9 -]{7,10}$" file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.