我想翻译这个JS正则表达式
^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7, 10}$
在 grep 中使用。以下所有电话号码均与其匹配:
+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
我尝试了很多变体,但没有一个适合我。
最佳答案
我还没有测试过你的模式,但需要考虑的一些事情是:
[\- ]
应改为 [- ]
或 [ -]
(in a character class
[]
escaping dash is unnecessary if it's first or last).
[\d]
可能会失败,因此请使用 [0-9]
或 [:digit:]
所以这给你留下了:
grep -E "^((8|\+7)[ -]?)?(\(?[0-9]{3}\)?[ -]?)?[0-9 -]{7,10}$" file
或egrep
:
egrep "^((8|\+7)[ -]?)?(\(?[0-9]{3}\)?[ -]?)?[0-9 -]{7,10}$" file
关于javascript - 翻译正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539885/