简体   繁体   中英

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? Thanks

This is because you use .matches() . What you want is .find() .

.matches() is a misnomer; it will try and match against the entire input . Regex matching is done using .find() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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