I think I got u, you wont stablish the pattern in the beginning. But how do u know what would be a pattern? For example the first sentence
aaa bbb cc
the second
aa bb bcc
In this case would you say that the first pattern is aa? or bbb?
What I want to tell is that you need to define for example if the blank space will be part of the pattern.





by: javaexpertoPosted on 2009-03-26 at 14:24:14ID: 23995495
In java you can use Pattern and Matcher classes.
With Pattern class you stablish your pattern that u'll look for. With Matcher class you pass your input or string where the patterns are.
For example in the code below, you are looking for the pattern a*b, that's means cero or mora 'a' followed by one b.
The String you look for that pattern is "aaaaab". Finally you ask with Matcher object if there is any pattern and save the result into a boolean.
If you want the words instead of just know if there is a pattern, then u can use split method from String class, it method will return an array with the "words" matched in your string, for example
String s = "aaaaab"
String arrayResult[] = s.split("a*b")
Select allOpen in new window