如何为复杂的印尼电话号码格式构建正则表达式? [英] How to build regex for complex Indonesian phone number format?
问题描述
最近,我正在使用 regexpal 来构建此自定义正则表达式. 我正在使用印尼电话号码的多个测试用例.
Recently, i am using regexpal to build this custom regex. I am working with several test case for Indonesian phone number.
以下是简单的08xx-3456-7890
或08xx34567890
但是如果我得到以下格式,可能会有点混乱
but it can be a bit confusing if i get this following format
here is my phone (08xx)34567890
08xx.3456.7890
08xx 3456 7890
(+62) 8xx34567890
(+62) 8xx-3456-7890
+628xx34567890
+62-8xx-3456-7890
+628xx 3456 7890
这是我用(08|628|62)[\s\)\-]*(\s|(\d){3,})
完成的正则表达式
但是我无法涵盖所有这些示例.
here is regex i have done with (08|628|62)[\s\)\-]*(\s|(\d){3,})
but i can not cover all of those sample.
+62是国家/地区代码
+62 is country code
您能提供任何解决方案来验证这些格式吗?
Can you please help me with any solution to validate those format?
电话号码可能包含字符串而不是数字,因为它是句子的一部分
the phone number is possible contains string instead just number, because it is part of sentence
推荐答案
您可以先尝试使用正则表达式[().- ]
删除令牌,然后用空字符串替换.然后,您可以使用 (?:\+62)?0?8\d{2}(\d{8})
来匹配电话号码.这与可选的+62
,可选的0
,8
,两位数字(xx
)和电话号码匹配:8位数字.组1包含电话号码.
You can try to remove tokens first with regex [().- ]
and replace with empty string. Then you can use (?:\+62)?0?8\d{2}(\d{8})
to match a phone number. This matches an optional +62
, an optional 0
, 8
, two digits (xx
) and the phone number: 8 digits. Group 1 contains the phone number.
这篇关于如何为复杂的印尼电话号码格式构建正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!