Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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,my string my_string的内容可以采用以下格式: bla bla…这是产品bla bla的id:31 5 2 0000 12请验证bla bla bla 或 或 我想从字符串中提取产品ID。上面示例中的产品ID为31 5 2 0000 12 产品ID的格式是以随机数开头。长度是无限的,数字之间的空格也是任意的 我当前提取产品ID的代码是: Pattern pattern = Pattern.compile("^#\\d+(\\s+\\d+)*$"); Matcher matcher

my string my_string的内容可以采用以下格式:

bla bla…这是产品bla bla的id:31 5 2 0000 12请验证bla bla bla

我想从字符串中提取产品ID。上面示例中的产品ID为31 5 2 0000 12

产品ID的格式是以随机数开头。长度是无限的,数字之间的空格也是任意的

我当前提取产品ID的代码是:

Pattern pattern = Pattern.compile("^#\\d+(\\s+\\d+)*$");
Matcher matcher = pattern.matcher(MY_STRING);
if(phoneNrMatcher.find()){
    System.out.println(matcher.group(0));                   
}
但它不起作用,谁能帮我找出哪里出了问题?可能是正则表达式

注:

-在我的示例中,ID 31 5 2 0000 12之前和之后的内容是任意的

-产品ID字符串始终以开头,后面紧跟一个数字,不带空格或其他字符。请尝试以下操作:

输出:

说明:

在这种情况下,您不需要在模式中提及行的开头或结尾。 此外,我的示例中的模式允许您在同一个字符串中查找多个id,前提是它们由一个既不是空格也不是数字的字符分隔

试试这个:

输出:

说明:

在这种情况下,您不需要在模式中提及行的开头或结尾。
此外,我的示例中的模式允许您在同一个字符串中查找多个id,前提是它们由一个既不是空格也不是数字的字符分隔

您的regex^和$有输入锚的开头和结尾。移除它们

输入锚点的开头使得正则表达式不能匹配输入开头以外的任何地方,顾名思义;输入锚点的结尾使。。。你明白了。除此之外,正则表达式还可以

顺便说一句,你可以只使用.group,它与.group0相同


顺便说一句2:如果一个输入中有多个数字,则在m上循环。find

您的regex^和$有输入锚定的开始和结束。移除它们

输入锚点的开头使得正则表达式不能匹配输入开头以外的任何地方,顾名思义;输入锚点的结尾使。。。你明白了。除此之外,正则表达式还可以

顺便说一句,你可以只使用.group,它与.group0相同


顺便说一句2:如果你在一个输入中有几个数字,在m上循环。find

你能解释一下你的答案而不是仅仅抛出答案吗?问题是它在Hello中也会匹配world@Mellon更深入的解释补充道。如果您发现fge的评论与此相关,请告诉我,我将改进模式。是的,@fge的评论非常重要,产品ID字符串总是以开头,后面紧跟一个数字,没有空格或其他字符。你能根据这个更新你的答案吗?谢谢@Mellon只是像我说的那样删除现有模式中的“^”和“$”就可以了。请你解释一下你的答案,而不是仅仅抛出答案?问题是它也会匹配Helloworld@Mellon更深入的解释补充道。如果您发现fge的评论与此相关,请告诉我,我将改进模式。是的,@fge的评论非常重要,产品ID字符串总是以开头,后面紧跟一个数字,没有空格或其他字符。你能根据这个更新你的答案吗?谢谢@Mellon只需删除现有模式中的^和$,就像我说的那样,就可以完成这个技巧。另外,如果有人想理解正则表达式,这个解释者可以帮助:完美。此外,如果有人希望理解正则表达式,此解释者可以帮助:
bla bla...this is the id of product bla bla: #31 5 2 0000 12 please verify bla bla...
Pattern pattern = Pattern.compile("^#\\d+(\\s+\\d+)*$");
Matcher matcher = pattern.matcher(MY_STRING);
if(phoneNrMatcher.find()){
    System.out.println(matcher.group(0));                   
}
String test = "bla bla...this is the tag id of product: #31 5 2 0000 12, please verify bla bla...";
// explanation of the Pattern:
//                                |starts with "#"
//                                | |directly followed by digits only
//                                | |   |character class including digits or spaces
//                                | |   |       |ad lib (greedy quantifier)
Pattern pattern = Pattern.compile("#\\d+[\\d\\s]+");

Matcher matcher = pattern.matcher(test);
// using a while group here so you may have multiple matches
while (matcher.find()) {
    System.out.println(matcher.group());
}
#31 5 2 0000 12