gpt4 book ai didi

java - 挪威数字的正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:41 25 4
gpt4 key购买 nike

我正在尝试编写匹配挪威所有电话号码的正则表达式。这意味着该号码可以以 +47、0047、47 或没有国家代码开头。为此,我使用以下正则表达式:

Pattern.compile("^((0047)?|(\"+47)?|(47)?)\"d{8}$")

问题是它永远不会匹配。我正在使用以下有效示例对其进行测试:

90909090,   normal number
4790909090, number with country code
+4790909090, country code using +
004790909090, country code using 00

无效:

+47909090, without country code or too short number
9090909o, invalid character
9090909, too few digits
+4690909090, wrong country code
909090909, too many digits
00474790909090 Trying to fool the regex now

最佳答案

认为你正在寻找

(0047|\+47|47)?\d{8} 

在您的 Java 表达式中将是:

Pattern.compile("(0047|\\+47|47)?\\d{8}"); 

关于java - 挪威数字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com