The Java String class has a matches(String regex) method that checks whether the current string matches the given regular expression. However, how can we find whether a substring of the current string would match the regular expression?
I am trying to mimic the behavior of grep using Java. grep takes a line and prints all those lines that match the given regular expression anywhere in the line. I am not sure how to do that with Java, because the matches method checks whether the entire line (i.e. string) matches the regex and not any substring within it.
Like ColinD said, your best bet is to read in your strings a line at a time, then run your regex with a reluctant wildcard on both ends.