简体   繁体   English

简单的Java正则表达式不匹配

[英]Simple java regex not matching

my regex returning false, cant understand why. 我的正则表达式返回假,无法理解原因。

Pattern patt = Pattern.compile("\\d+");
patt.matcher("a 18c1").matches(); //returning false

Also I tried [0-9]+ , (\\\\d+) , ([0-9]+) , they didnt work too.. Can you help me? 我也试过[0-9]+(\\\\d+)([0-9]+) ,它们也没有用。 Thanks 谢谢

This is because you use .matches() . 这是因为您使用.matches() What you want is .find() . 您想要的是.find()

.matches() is a misnomer; .matches()是用词不当; it will try and match against the entire input . 它将尝试与整个输入匹配。 Regex matching is done using .find() : 正则表达式匹配使用.find()

final Matcher matcher = patt.matcher("a 18c1");
if (matcher.find())
    System.out.println(matcher.group());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM