java - Groups in Pattern compilers and Matchers -


so playing around groups in patterns , matchers , when dealing multiple regexs in pattern compiler there particular group call don't understand.

here's it.

pattern p=pattern.compile("((\\s+)(\\s+))");         matcher m=p.matcher("hello people yeah");         while (m.find()){             string line=m.group(2);             system.out.println("first match "+line);         } 

so can see have regex in compiler, first 1 looks non-whitespaces , second 1 looks whitespaces.

now far can understand group 0 whole expression, group 1 ((\s+)(\s+)) group 2 is/supposed (\s+) , group 3 (\s+)

however when call group 2, hello people

but not yeah weird because if compiler ("\s+") hello people yeah

so yeah don't understand why yeah not returned when call group 2.

what regex searching is:

  • one or more non-whitespace characters, followed by
  • one or more whitespace characters.

now, match twice in string:

  • first match: [group 2 - hello, group 3 - " "]
  • second match: [group 2 - people, group 3 - " "]

it doesn't match yeah, because there no whitespace character after it. so, match fails.

however, if change regex - ((\\s*)(\\s*)), match yeah, because \\s* matches 0 or more whitespace characters, , hence match 0 whitespace character too.


issue regex apart. problem can solved simple string#split() method. don't need matcher , pattern class.

string[] words = str.split("\\s+"); 

the above code splits string on 1 or more whitespaces. array contain 3 words.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -