simple java regex throwing illegalstateexception -
this question has answer here:
- simple java regex matcher not working 2 answers
im trying quick sanity check... , failing. here code -
import java.util.regex.*; public class tester { public static void main(string[] args) { string s = "a"; pattern p = pattern.compile("^(a)$"); matcher m = p.matcher(s); system.out.println("group 1: " +m.group(1)); } }
and expect see group 1: a
. instead illegalstateexception: no match found
, have no idea why.
edit: tries printing out groupcount()
, says there 1.
you need invoke m.find()
or m.matches()
first able use m.group
.
find
can used find each substring matches pattern (used in situations there more 1 match)matches
check if entire string matches pattern wont need add^
,$
in pattern.
we can use m.lookingat()
lets skip description (you can read in documentation).
Comments
Post a Comment