I have to parse the output of a program to search for errors. The errors are indicated as:
[(FieldName/Value) = (phrase/What is Up John Carl?) failed rule alphanumeric] [(FieldName/Value) = (newLabel/Óscar's IPad) failed rule illegalchars]
There can be more than one error per line, and for each error I want to retrieve the words/sentences in bold. In order to do that I'm creating a regular expression as follows:
static String s1 = Pattern.quote("[(FieldName/Value) = (");
static String s2 = Pattern.quote(") failed rule");
static String s3 = Pattern.quote("]");
static Pattern p = Pattern.compile(s1 + "(\\w+)/(.+)" + s2 + "(.+)" + s3);
while (matcher.find()) {
String token = matcher.group(1);
sb.append("#");
sb.append(token);
token = matcher.group(2);
sb.append("#");
sb.append(token);
token = matcher.group(3).trim();
sb.append("#");
sb.append(token);
}
But the output is :
#phrase#What is Up John Carl?) failed rule alphanumeric] [(FieldName/Value) = (newLabel/Óscar's IPad#illegalchars
So it is not returning two matches, just one. It is matching the second group to the rest of the string, instead of stopping at the first "failed rule"
. I suppose it is due to the first (.+)
in the pattern, but the thing is that anything can go in there, so I need the (.+)
. Any ideas how to do it?