Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java正则表达式只匹配0-9之间的列表,顺序很重要_Java_Regex_List - Fatal编程技术网

java正则表达式只匹配0-9之间的列表,顺序很重要

java正则表达式只匹配0-9之间的列表,顺序很重要,java,regex,list,Java,Regex,List,我想用java开发一个只匹配0-9sequence的正则表达式 [0, 1, 2] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] --> match [7, 8] [0, 1] [0, 1, 2, 3] [0, 1, 2, 3] [4, 5, 6, 7, 8, 9] [4] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] --> match [1] 你想用正则表达式来做这件事很奇怪,所以我可以想出这个主意: List<Integer> n

我想用java开发一个只匹配0-9sequence的正则表达式

[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  --> match
[7, 8]
[0, 1]
[0, 1, 2, 3]
[0, 1, 2, 3]
[4, 5, 6, 7, 8, 9]
[4]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] --> match
[1]

你想用正则表达式来做这件事很奇怪,所以我可以想出这个主意:

List<Integer> numbers = Arrays.asList(0,1,2,3,4,5,6,7,8,9);
String regex = "0123456789";

String listStr = numbers.stream()
                        .map(number -> String.valueOf(number))
                        .collect(toStringJoiner(""));

if (listStr.matches(regex))
    System.out.println("match");
else
    System.out.println("not match"); 

为什么要为这个使用regexp?只需使用纯文本比较,这里没有需要正则表达式的复杂性。我需要使用regexp来匹配[0,1,2,3,4,5,6,7,8,9],并反向匹配其余列表。您是否尝试过此regex 0123456789?如果列表为01234567,89,此解决方案将失败。我不明白为什么字符串转换是必要的。对于一个简单的任务来说,似乎有很多工作要做。另一件事:ToString Joiner似乎出现在预览版Java 8 b00中,但它已改为Collectors.Join正式发布。
if (listStr.matches(regex))    // regex approach
if (listStr.equals(regex))     // or equals approach
                               (not sure why you want a regex)