I want to retrieve a strings from a global string via Matcher & Pattern using REGEX.
String str = \"ABC123DEF<
You need a non greedy regex:
Pattern pattern = Pattern.compile(".*?");
Use ?
to specify non greedy. This means it will match the first match it finds instead of the outer most match...
If you only want ABC
and DEF
then you can do something like this using lookaheads and lookbehinds:
String str = "ABC123DEF";
Pattern pattern = Pattern.compile("((?<=).*?(?=))");
Matcher matcher = pattern.matcher(str);
while(matcher.find())
{
System.out.println(matcher.group());
}
If you do a google search you should be able to find information on lookaheads and lookbehinds...