simple java regex throwing illegalstateexception -


this question has answer here:

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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -