简体   繁体   English

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

[英]Regex to match a number separated with multiple comma

I'm working on identifying patterns using regex. 我正在使用正则表达式识别模式。

My use-case is to find strings of the following format 我的用例是找到以下格式的字符串

100,253,2586,3654

Here is what I've tried 这是我尝试过的

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

but it doesn't seem to work properly. 但它似乎没有正常工作。

Help me figure out the issue. 帮我解决这个问题。

Further to comments: 进一步评论:
pattern Should identify 模式应该识别

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)

Try something like this : 尝试这样的事情:

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

Edit : Regex explanation. 编辑:正则表达式解释。

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

\\\\d+ ==> matches one or more digits. \\\\d+ ==>匹配一个或多个数字。

(?<!^,) ==> Negative look-behind. (?<!^,) ==>负面的后视。 Checks if String doesn't start with a ",". 检查String是否以“,”开头。

(,(?!$)|$))+") ==> checks digits are - either followed by a comma and NOT followed by end of String OR followed by end of String. (,(?!$)|$))+") ==>检查数字 - 后跟逗号,后跟字符串结尾或后跟字符串结尾。

This will 这将

  1. Prevent a comma at the beginning. 在开头防止逗号。
  2. prevent comma at the end. 最后防止逗号。
  3. multiple commas 多个逗号

This should match the numerics you wish to grab. 这应该与您想要获取的数字相匹配。 Note that it allows a trailing decimal point since you didn't specify anything about that consideration: 请注意,它允许使用尾随小数点,因为您没有指定有关该考虑因素的任何内容:

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

Perhaps you need some lookaround to guarantee that matches are neither preceded nor followed by whatever characters you would have invalidate the match. 也许您需要一些外观来保证匹配既不会先于也不会跟随您将使匹配无效的任何字符。 But can't you have a period or comma as regular punctuation immediately after a number in a typical body of text? 但是,在典型的正文文本中,您不能在一个数字之后立即使用句点或逗号作为常规标点符号吗?

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

这应该做它you.Your正则表达式不会匹配只有123 。如果这不是一个可能的情况下使用

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

use this regex: 使用这个正则表达式:

[^\,](\d+\.?,?)+[^\,]$

This should apply to your given cases 这适用于您的特定情况

I had trouble getting @TheLostMind's answer to work when I was trying to match the number within a string of other letters. 当我试图在一串其他字母中匹配数字时,我无法获得@ TheLostMind的工作答案。 I ended up going with: 我结束了:

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

(will need to escape those backslashes to use in your pattern string, ie \\\\d+(,\\\\d+)*(\\\\.\\\\d+)? ) (需要转义那些在模式字符串中使用的反斜杠,即\\\\d+(,\\\\d+)*(\\\\.\\\\d+)?

Note : This doesn't quite answer the OP, as it doesn't check for no leading or trailing comma directly, but if the number does have that, it won't pull it from the CharSequence. 注意 :这并不能完全回答OP,因为它不会直接检查前导或尾随逗号,但如果数字确实有,则不会从CharSequence中提取它。

Explanation: 说明:

\\d+ matches one or many numbers \\d+匹配一个或多个数字

(,\\d+)* matches zero or more groups of "a comma followed by one or many numbers" (,\\d+)*匹配零个或多个“逗号后跟一个或多个数字”的组

(\\.\\d+)? matches zero or one groups of a decimal point followed by one or many numbers 匹配零个或一个小数点组,后跟一个或多个数字

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());

Prints: 打印:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM