matcher.find() 匹配太多
我对 Matcher 的 find() 方法返回的匹配项比我想象的正则表达式会创建的匹配项多感到困惑。下面是我编写的 JUnit 测试,试图解决这个问题。所有测试都通过了,但我不明白为什么我的 find() 返回匹配其 group( 1 ) 值为 null 或空的内容(请参阅 // Why more find()s thanactual matches?commed in the code)?
public class JustTheDigits {
@Test
public void testJustTheDigits() {
doTest( "DV_APLCN: 563 ,DV_DHR_APLCN: 5632, PIC_NOTE: 6254", new ArrayList<Integer>( Arrays.asList( 563, 5632, 6254 ) ) );
doTest( "563 ,DV_DHR_APLCN: 5632, PIC_NOTE", new ArrayList<Integer>( Arrays.asList( 563, 5632 ) ) );
doTest( "hello 563 jello", new ArrayList<Integer>( Arrays.asList( 563 ) ) );
doTest( "Hello World", new ArrayList<Integer>() );
}
private void doTest( String candidate, List<Integer> expected ) {
List<Integer> actual = justTheDigits( candidate );
assertEquals( expected, actual );
}
private static Pattern pattern = Pattern.compile( "(\\d+)?" );
public List<Integer> justTheDigits( String input ) {
List<Integer> listOfDigits = new ArrayList<Integer>();
Matcher matcher = pattern.matcher( input );
while ( matcher.find() ) {
String s = matcher.group( 1 );
// Why more find()s than actual matches?
if ( s != null && "".equals( s ) == false ) {
listOfDigits.add( Integer.parseInt( s ) );
}
}
return listOfDigits;
}
}
如果有的话,我可以对我的正则表达式做些什么来避免该死的非空或空检查?
I am confused by my Matcher's find() method returning more matches than I thought my regex would create. Below is the JUnit test I wrote to try to hash this out. All the tests pass, but I don't understand why my find() return matches whose group( 1 ) value is null or empty (see // Why more find()s than actual matches? commend in the code)?
public class JustTheDigits {
@Test
public void testJustTheDigits() {
doTest( "DV_APLCN: 563 ,DV_DHR_APLCN: 5632, PIC_NOTE: 6254", new ArrayList<Integer>( Arrays.asList( 563, 5632, 6254 ) ) );
doTest( "563 ,DV_DHR_APLCN: 5632, PIC_NOTE", new ArrayList<Integer>( Arrays.asList( 563, 5632 ) ) );
doTest( "hello 563 jello", new ArrayList<Integer>( Arrays.asList( 563 ) ) );
doTest( "Hello World", new ArrayList<Integer>() );
}
private void doTest( String candidate, List<Integer> expected ) {
List<Integer> actual = justTheDigits( candidate );
assertEquals( expected, actual );
}
private static Pattern pattern = Pattern.compile( "(\\d+)?" );
public List<Integer> justTheDigits( String input ) {
List<Integer> listOfDigits = new ArrayList<Integer>();
Matcher matcher = pattern.matcher( input );
while ( matcher.find() ) {
String s = matcher.group( 1 );
// Why more find()s than actual matches?
if ( s != null && "".equals( s ) == false ) {
listOfDigits.add( Integer.parseInt( s ) );
}
}
return listOfDigits;
}
}
What, if anything, can I do to my regex to avoid the bloody not null or empty check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除
?
,以便您的正则表达式只是(\\d+)
,因为当存在一系列一个或多个数字、一次或多次时,您的正则表达式会匹配。即使没有数字,正则表达式仍然会匹配。Remove the
?
so that your regex is simply(\\d+)
as your regex is matching when there is a series of one or more digits, one or more times. Even if there are no digits, the regex will still match.这 ?问号表示前面的语句是可选的。从字面意义上讲,您要求的是任意数字中至少 1 个中的 0 或 1。
将模式更改为
"\\d+"
它应该可以正常工作。The ? question mark says the previous statement is optional. In literal terms you are asking for 0 or 1 of at least 1 of any digit.
Change the pattern to
"\\d+"
and it should work fine.阅读此内容: http://docs.oracle.com/javase/tutorial/ Essential/regex/quant.html
我相信您是说您希望一组任何一个或多个数字出现一次或根本不出现。这就是为什么如果没有找到它仍然会返回一个匹配项。我不确定你到底想做什么,但我认为你可能会同意“\d+”
read this: http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
i believe you are saying you want a group of any one or more digits to show up once or not at all. that is why if it doesn't find it it will still return a match. i'm not sure exactly what you want to do, but i think you'd probably be fine with "\d+"