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

标签 java regex

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

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

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)

最佳答案

尝试这样的事情:

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

编辑:正则表达式解释。

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

\\d+ ==> matches one or more digits.

(?<!^,) ==> Negative look-behind. Checks if String doesn't start with a ",".

(,(?!$)|$))+") ==> checks digits are - either followed by a comma and NOT followed by end of String OR followed by end of String.

这将

  1. Prevent a comma at the beginning.
  2. prevent comma at the end.
  3. multiple commas

关于java - 正则表达式匹配用多个逗号分隔的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034857/

相关文章:

java - 替代列表数据结构

javascript - 匹配开头或结尾带有特殊字符的单词

regex - vimrc 检测远程连接

java正则表达式转义oracle sql特殊字符

java - 在这种情况下使用枚举是否正确?

java - 无法在 Eclipse 中创建 .dslr

java - 有效地比较数组中的任何这些值

java - JDialog 用于状态显示。 .

ruby - 匹配正则表达式中的确切短语和单词

javascript - 如何在 split() javascript 中排除转义字符