0

I know how to use regex in order to figure out whether a string matches a certain pattern in the following way:

String string1 = "One two .* four";
String string2 = "One two three four";
if(Pattern.matches(string1, string2)) System.out.println("It matches");
else System.out.println("It doesn't match");

Prints:

It matches

My question is, how can I make it print/store "three", as in, how do I make it print/store the part of the string that matched the regex, and leave out everything else. Preferably, it would stop after finding the first one so I could add several of these differences in an array.

Thank you for the help.

0 Answers0