Matching one string multiple times using regex in Java
I'm having some issues with making the following regex work. I would like
the following string:
"Please enter your name here"
to result in an array with the following elements:
'please enter', 'enter your', 'your name', 'name here'
Currently, I'm using the following pattern, and then creating a matcher
and iterating in the following way:
Pattern word = Pattern.compile("[\w]+ [\w]+");
Matcher m = word.matcher("Please enter your name here");
while (m.find()) {
wordList.add(m.group());
}
But the result I'm getting is:
'please enter', 'your name'
What am I doing wrong? (P.s., i checked the same regex on regexpal.com and
had the same problem). It seems like the same word won't be matched twice.
What can I do to achieve the result I want?
Thanks.
No comments:
Post a Comment