使用正则表达式匹配多个逗号分隔的数字

4

我正在使用正则表达式来识别模式。

我的用例是查找以下格式的字符串

100,253,2586,3654

以下是我尝试过的方法:

(\d+\,+\d+\,*)+

但它似乎不能正常工作。

帮我找出问题所在。

进一步评论:
模式应该被识别。

1. Any combination of numbers separated by a comma  (eg: 123,465,798)  
2. shouldn't begin or end with comma (eg: ,123,46 )  
3. Decimals (eg: 123,465.2324)

你可以从字符串中提取数字,使用正则表达式\d+ - Tushar
2
如果你想得到一个很好的答案,就必须更明确地说明你要匹配什么。任何数字和逗号的混合都可以吗?我们可以以逗号开头吗?我们可以以逗号结尾吗?我们允许连续两个逗号吗?在逗号之间允许任意数量的数字吗?它可以以零开头吗?等等。如果你对你的需求不明确,那么简单的[\d,]+与任何更复杂的答案一样正确。 - Dawood ibn Kareem
你是将正则表达式应用于独立的字符串,还是在较大的字符串中搜索匹配项? - Alan Moore
在一个较大的文本块(比如一段话)中搜索匹配项。 - Alekhya Vemavarapu
5个回答

4
试试像这样做:

尝试像这样做:

public static void main(String[] args) {
    String s = "100,253,2586,3654";
    System.out.println(s.matches("((?<!^,)\\d+(,(?!$)|$))+"));
}

编辑:正则表达式解释。

((?<!^,)\\d+(,(?!$)|$))+") ==>

\\d+ ==> 匹配一个或多个数字。

(?<!^,) ==> 负向先行断言。检查字符串是否不以“,”开头。

(,(?!$)|$))+") ==> 检查数字是 - 要么后跟逗号并且不跟随字符串结尾,要么后跟字符串结尾。

这将

  1. 防止逗号在开头。
  2. 防止逗号在结尾。
  3. 多个逗号。

谢谢。它有效了。您能否添加一些正则表达式的文档,这也将有助于其他人。另外,我该如何使其适用于像100,253,2586,3654.999这样的小数? - Alekhya Vemavarapu
1
@AlekhyaVemavarapu - 我已经更新了我的答案,解释了使用的正则表达式。我会研究一下如何检查小数点。 - TheLostMind
没有运气,Vinod。感谢你的努力。 - Alekhya Vemavarapu
@AlekhyaVemavarapu - 你能给我展示一下输入吗?我的正则表达式适用于 "100,253,2586.3654"。 - TheLostMind
10,000,000,253,023,253,365,2536,2563,253和10,000,456,258.99 - Alekhya Vemavarapu
显示剩余2条评论

1
这应该与您希望获取的数字匹配。请注意,它允许有一个尾随小数点,因为您没有指定任何相关考虑:
\d+(,\d+)*(\.\d*)?

也许您需要一些环视来确保匹配项既不在之前也不在之后包含任何使匹配无效的字符。但是在典型文本正文中,数字后面紧跟着句号或逗号作为常规标点符号,这样行不行?

0
^\d+(?:,\d+)*$

这应该对你有用。你的正则表达式将不仅匹配 123 。如果这不是可能的情况,请使用

^\d+(?:,\d+)+$

0
使用这个正则表达式:
[^\,](\d+\.?,?)+[^\,]$

这应该适用于您提供的情况


它也会匹配 123,,这可能是一个有效的测试用例,也可能不是。 - Kenneth K.
抱歉,当我写答案时他没有指定。 - PekosoG

0

当我尝试在其他字母的字符串中匹配数字时,我遇到了麻烦,无法让@TheLostMind的答案起作用。最终我选择了以下方法:

 \d+(,\d+)*(\.\d+)?

(需要转义这些反斜杠以在模式字符串中使用,即\\d+(,\\d+)*(\\.\\d+)?)

注意:这并没有完全回答OP的问题,因为它没有直接检查是否有前导或尾随逗号,但如果数字确实有这个,它不会从CharSequence中提取它。

解释:

\d+匹配一个或多个数字

(,\d+)*匹配零个或多个“逗号后跟一个或多个数字”的组

(\.\d+)?匹配零个或一个小数点后跟一个或多个数字的组

Pattern pattern = Pattern.compile("\\d+(,\\d+)*(\\.\\d+)?");
Matcher m1 = pattern.matcher("100,253,2586,3654");
Matcher m2 = pattern.matcher("100,253,2586,3654.999");
Matcher m3 = pattern.matcher("10,000,000,253,023,253,365,2536,2563,253");
Matcher m4 = pattern.matcher("10,000,456,258.99");
Matcher m5 = pattern.matcher("10.99");
Matcher m6 = pattern.matcher("1");
Matcher m7 = pattern.matcher("Text on the left 100,253,2586,3654.00000 text on the right");
Matcher m8 = pattern.matcher("Text on the left ,100,253,2586,3654.00000, text on the right");

System.out.println("Matcher 1 matches " + m1.find() + " result: " + m1.group());
System.out.println("Matcher 2 matches " + m2.find() + " result: " + m2.group());
System.out.println("Matcher 3 matches " + m3.find() + " result: " + m3.group());
System.out.println("Matcher 4 matches " + m4.find() + " result: " + m4.group());
System.out.println("Matcher 5 matches " + m5.find() + " result: " + m5.group());
System.out.println("Matcher 6 matches " + m6.find() + " result: " + m6.group());
System.out.println("Matcher 7 matches " + m7.find() + " result: " + m7.group());
System.out.println("Matcher 8 matches " + m8.find() + " result: " + m8.group());

输出:

Matcher 1 matches true result: 100,253,2586,3654
Matcher 2 matches true result: 100,253,2586,3654.999
Matcher 3 matches true result: 10,000,000,253,023,253,365,2536,2563,253
Matcher 4 matches true result: 10,000,456,258.99
Matcher 5 matches true result: 10.99
Matcher 6 matches true result: 1
Matcher 7 matches true result: 100,253,2586,3654.00000
Matcher 8 matches true result: 100,253,2586,3654.00000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接