Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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_String - Fatal编程技术网

Java正则表达式从给定字符串中提取单词

Java正则表达式从给定字符串中提取单词,java,regex,string,Java,Regex,String,我需要从给定的字符串中提取一个单词。在这种情况下,字符串可能以不同的方式出现。e、 g: “签名指的是测试id 69043 abcd。签名指的是测试id 1001” “签名指的是测试id 69043定义。签名指的是测试id 1001“ “签名指的是测试 识别号69043。” 此外,测试id可能不总是小写。如果我可以忽略它是小写还是大写,那就更好了。它可以是测试ID,也可以是测试ID 我暂时写了这个“测试id([0-9]+)” 我想从这些给定字符串中提取“测试id号”。有时字符串中可能有多个“测试

我需要从给定的字符串中提取一个单词。在这种情况下,字符串可能以不同的方式出现。e、 g:

“签名指的是测试id 69043 abcd。签名指的是测试id 1001”

“签名指的是测试id

69043定义。签名指的是测试id 1001“

“签名指的是测试

识别号69043。”

此外,测试id可能不总是小写。如果我可以忽略它是小写还是大写,那就更好了。它可以是测试ID,也可以是测试ID

我暂时写了这个“测试id([0-9]+)”


我想从这些给定字符串中提取“测试id号”。有时字符串中可能有多个“测试id号”。某些时间字符串有多行,因为它出现在一个段落中

您可以在这里使用正式的Java模式匹配器,使用以下不区分大小写的模式:

(?i)test\s+id\s+(\d+)
考虑以下代码段:

String input = "The Signature refers to test id 69043 abcd. ";
input += "The Signature refers to test id 1001";
String pattern = "(?i)test\\s+id\\s+(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

while (m.find()) {
   System.out.println("Found value: " + m.group(1) );
}
这将正确输出两个ID:

Found value: 69043
Found value: 1001

您可以在这里使用正式的Java模式匹配器,使用以下不区分大小写的模式:

(?i)test\s+id\s+(\d+)
考虑以下代码段:

String input = "The Signature refers to test id 69043 abcd. ";
input += "The Signature refers to test id 1001";
String pattern = "(?i)test\\s+id\\s+(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

while (m.find()) {
   System.out.println("Found value: " + m.group(1) );
}
这将正确输出两个ID:

Found value: 69043
Found value: 1001

检查此答案:[链接]。应该会有帮助。请检查此答案:[链接]。应该会有帮助。如果文本和digit@MadushanChathuranga我只是在演示中添加了一些新行,它仍然有效。唯一的问题是,如果你想跨新行匹配某些内容,但这里似乎不需要,我想跨新行匹配。在某些情况下,值可能拆分为太多新行,并且它可能不是“测试id”,也可能是“测试id”,“测试id”。感谢you@MadushanChathuranga我使模式不区分大小写,现在,即使在多行之间拆分,它也可以匹配目标模式。
\s
允许使用不同类型的空白字符,包括
EOL
如果文本和digit@MadushanChathuranga我只是在演示中添加了一些新行,它仍然有效。唯一的问题是,如果你想跨新行匹配某些内容,但这里似乎不需要,我想跨新行匹配。在某些情况下,值可能拆分为太多新行,并且它可能不是“测试id”,也可能是“测试id”,“测试id”。感谢you@MadushanChathuranga我使模式不区分大小写,现在即使在多行中拆分,它也可以匹配目标模式。
\s
允许使用不同类型的空白字符,包括
EOL