Java正则表达式没有';我找不到匹配的
我有Java正则表达式问题。我需要匹配的字符串如下模式:Java正则表达式没有';我找不到匹配的,java,regex,Java,Regex,我有Java正则表达式问题。我需要匹配的字符串如下模式:378 Columbian Forecast Yr-NB-Q-Columbian_NB我需要提取第一个和第二个之间的内容- Pattern modelRegEx = Pattern.compile("[^-]{15,}[^-]"); Matcher m = modelRegEx.matcher(temp); String model = m.group(0); 下面是我在这个正则表达式后面的推理[^-]{15,}[^-]: 我只想要连字符
378 Columbian Forecast Yr-NB-Q-Columbian_NB
我需要提取第一个和第二个之间的内容-
Pattern modelRegEx = Pattern.compile("[^-]{15,}[^-]");
Matcher m = modelRegEx.matcher(temp);
String model = m.group(0);
下面是我在这个正则表达式后面的推理[^-]{15,}[^-]:
我只想要连字符之间的内容,所以我使用了[^-]
。在连字符之间有多个文本实例,所以我选择了一个足够大的数字,它在较小的匹配项上不会出现。所以我用了{15,}
我的错误:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:496)
at alfaSpecificEditCheck.tabTest.main(tabTest.java:21)
当我根据这里的字符串测试我的正则表达式模式时:模式匹配。当我特别针对Java()使用这个测试仪进行测试时,结果是没有找到匹配项 您需要让正则表达式引擎首先找到匹配项。一般来说,让我们迭代所有匹配的部分,我们使用
Pattern modelRegEx = Pattern.compile("[^-]{15,}[^-]");
Matcher m = modelRegEx.matcher(temp);
while(m.find()){// <-- add this
String model = m.group(0);
//do stuff with each match you will find
}
您需要让正则表达式引擎首先找到匹配项。一般来说,让我们迭代所有匹配的部分,我们使用
Pattern modelRegEx = Pattern.compile("[^-]{15,}[^-]");
Matcher m = modelRegEx.matcher(temp);
while(m.find()){// <-- add this
String model = m.group(0);
//do stuff with each match you will find
}