使用正则表达式解析文本中的电话号码
我想使用正则表达式解析来自世界各地许多国家的电话号码,我需要可以通过多种不同方式获取的电话号码,并且需要捕获所有电话号码。
例子。
00212 23254324
+212 324324324
232 2 121 343
234/2354323
12223223213
3123-321-321
带条、空格、破折号。
谢谢
I want to parse phone numbers from many countries around the world using a regex I need the phone numbers I can come in many different ways and need to catch them all.
Examples.
00212 23254324
+212 324324324
232 2 121 343
234/2354323
122 232 23 213
3123-321-321
With bars, spaces, dashes.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于存在多种具有不同分隔符、不同前缀和符号的电话号码格式这一事实,我认为正则表达式无法真正匹配所有这些情况(情况比您在那里列出的情况要多得多)。
如果您想验证它们,我建议您计算数字(忽略符号和分隔符)并检查是否有足够的数字来形成有效的电话号码。
这种方式更容易存储它们或将它们转换为您的格式:您可以从数字中提取所有数字,然后以您想要的方式格式化数字。
希望这有帮助!
Based on the fact that there are numerous telephone number formats with different separators, different prefixes and symbols, I don't think that regex can really match all of those cases (there are a lot more cases than you have listed there).
If you want to validate them, I suggest you count the digits (ignoring symbols and separators) and check if there are enough digits to form a valid phone number.
This way is much more easier to store them or convert them in your format : you can extract all the digits from the number and then format the number in what way you want.
Hope this helps!
您是否正在尝试解析电话号码以便拒绝无效电话号码?考虑到世界各地存在的多样性,这似乎可能会拒绝尽可能多的好价值观,因为它拒绝坏价值观(虽然没有出现在您的列表中,但我相信有些国家允许在电话号码中使用字母 AZ!)
Are you trying to parse the phone numbers so that you can reject invalid ones? That seems likely to reject as many good values as it rejects bad ones, given the variety that exists around the world (although not present in your list, there are I believe some countries that allow letters A-Z in their phone numbers!)