Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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正则表达式可选,管道不工作_Java_Regex - Fatal编程技术网

java正则表达式可选,管道不工作

java正则表达式可选,管道不工作,java,regex,Java,Regex,各位好,, 我有下面的正则表达式模式 String pattern1 = "([0-9+\\s|\\s])integer"; 我在下面的字符串上使用的。它假设选择整数和一个数字,后面跟一个空格,如果前面没有数字,则只选择空格。我想我一定很接近,但我已经花了一上午的时间。非常感谢您的帮助。 brtrval!78928458695整数33整数 问题是它可能是一个空空间+整数或数字+整数,没有特定的顺序。 我最接近的是 "\\d+\\s*integer(.*)" 但这并不意味着它是一种模式——它只

各位好,, 我有下面的正则表达式模式

String pattern1 = "([0-9+\\s|\\s])integer";
我在下面的字符串上使用的。它假设选择整数和一个数字,后面跟一个空格,如果前面没有数字,则只选择空格。我想我一定很接近,但我已经花了一上午的时间。非常感谢您的帮助。 brtrval!78928458695整数33整数


问题是它可能是一个空空间+整数或数字+整数,没有特定的顺序。 我最接近的是

"\\d+\\s*integer(.*)"
但这并不意味着它是一种模式——它只在整数前面有一个数字并且只获取字符串的其余部分时才起作用,但我希望将其作为一种模式。

移动]在9之后:

这与78928458695整数、整数和33整数匹配,假设我得到了您想要的

例如:

String s = "BRTR val !http://www.w3.org/2001/XMLSchema# 78928458695 integer integer 33 integer";
Pattern pattern = Pattern.compile("([0-9]+\\s|\\s)integer");
Matcher matcher = pattern.matcher(s);
List<String> list = new ArrayList<>();
while (matcher.find()) {
    list.add('"' + matcher.group() + '"');
}
System.out.println(list); // ["78928458695 integer", " integer", "33 integer"]

无论采用哪种方法,您都可以在整数之前检查空格,您可以将其从括号中删除

现在,您只需要寻找一个可选的整数,后跟一个空格,后跟“integer”,因此最终的正则表达式可以是:

[0-9]*\\s退出


这匹配0个或多个数字,后面跟一个空格,后面跟一个整数。

不幸的是,这个游戏只匹配一个没有数字的整数,我需要78928458695整数,整数,33整数。问题是它可能是一个空空格+整数或/和数字+整数。@user2917629它匹配这些,看看我添加的示例,这个组合也只给出整数。我在找78928458695整数,整数,33整数。问题是它可能是一个空的空格+整数和/或数字+整数,没有特定的顺序。你确定吗?因为我测试了它,输入1设置为BRTR val!http://www.w3.org/2001/XMLSchema 78928458695 integer 33 integer和正则表达式设置为[0-9]*\sinteger,它捕获所有三个,并返回“78928458695 integer”、“integer”和“33 integer”。是的,我刚刚再次测试。我正在使用Java7。它只返回空格+整数。我还使用Jmeter 2.7正则表达式进行测试,记录如下:
String s = "BRTR val !http://www.w3.org/2001/XMLSchema# 78928458695 integer integer 33 integer";
Pattern pattern = Pattern.compile("([0-9]+\\s|\\s)integer");
Matcher matcher = pattern.matcher(s);
List<String> list = new ArrayList<>();
while (matcher.find()) {
    list.add('"' + matcher.group() + '"');
}
System.out.println(list); // ["78928458695 integer", " integer", "33 integer"]