gpt4 book ai didi

Java 正则表达式 : For Phone number without leading space or 0 or +

转载 作者:行者123 更新时间:2023-12-02 02:28:36 26 4
gpt4 key购买 nike

我想在电话号码中开始实际数字字符之前删除前导 0 、 + 或任何空格 (\s)。可能这并不难,但由于我是 regix 的新手,所以我正在寻求帮助。我尝试自己制作,但没有成功。

这是一个类似的链接,但它也添加了前导+,但我不想要这样。 trim phone number with regex

所以我尝试了这个,但它也删除了内部 0

(^\s*|0*|[+]*)

我也尝试过这个,但它实际上不能在java中工作,而只能在Php中工作,所以我需要基于java的regix的帮助

^(?:\s*0+|[+]0*|(\d+)0*)(?!$)

输入示例

+490232345678
0049032345678
+1 (555) 234-5078
+7 (23) 45/6789+10
(0123) 345/5678, ext. 666

期望的输出

490232345678
49032345678
15552345678
72345678910
1233455678666

我只需要 regix,因为我已经知道如何在 java 中使用该 regix。我有这段代码需要regix

    String value = "+490232345678";
Pattern p = Pattern.compile("(^\s*|0*|[+]*)");
Matcher m = p.matcher(value);
value = m.replaceAll("");

最佳答案

试试这个:

public static void main(String[] args) {
String [] testResult = {"+490232345678",
"0049032345678",
"+1 (555) 234-5078",
"+7 (23) 45/6789+10",
"(0123) 345/5678, ext. 666"};
String reg = "^([\\(+ ]0| +|0+|\\(\\)|\\+)| +|[^\\d]+|/$";
for (String phone : testResult) {
System.out.println(phone.replaceAll(reg, ""));
}
}

输出将是:

490232345678
49032345678
15552345078
72345678910
1233455678666

更简单的方法是分两步完成:

 .replaceAll("[^\\d]+", "").replaceAll("^0+", "")

删除所有非数字,然后删除前导零。

正则表达式说明

^([\(+ ]0| +|0+|\(\)|\+)| +|[^\d]+|\/$

1st Alternative ^([\(+ ]0| +|0+|\(\)|\+)
^ asserts position at start of the string
1st Capturing Group ([\(+ ]0| +|0+|\(\)|\+)
1st Alternative [\(+ ]0
Match a single character present in the list below [\(+ ]
\( matches the character ( literally (case sensitive)
+ matches a single character in the list + (case sensitive)
0 matches the character 0 literally (case sensitive)
2nd Alternative +
+ matches the character literally (case sensitive)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
3rd Alternative 0+
0+ matches the character 0 literally (case sensitive)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
4th Alternative \(\)
\( matches the character ( literally (case sensitive)
\) matches the character ) literally (case sensitive)
5th Alternative \+
\+ matches the character + literally (case sensitive)
2nd Alternative +
+ matches the character literally (case sensitive)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
3rd Alternative [^\d]+
Match a single character not present in the list below [^\d]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equal to [0-9])
4th Alternative \/$
\/ matches the character / literally (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

解释来源:https://regex101.com/

这是 https://www.debuggex.com/ 的视觉表示

enter image description here

关于Java 正则表达式 : For Phone number without leading space or 0 or +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472516/

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