errorString="AxisFault\n
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException\n
faultSubcode: \n
faultString: My Error\n
faultActor: \n
faultNode: \n
faultDetail: \n
{}string: this is the fault detail"
Pattern pattern = Pattern.compile(".*faultString(.*)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(errorString);
if (matcher.matches()) {
String match = matcher.group(1);
return match;
}
I want to get “My Error”, but it returns to the end of the whole string instead of matching to the \n at the end of the faultString line. I’ve tried many techniques to get it to stop at the end of the line, without success.
thanks
You shouldn’t be passing
Pattern.DOTALL; that causes newlines to be matched by.*, which is exactly what you don’t want here.A better regex would be:
and then, instead of
matcher.matches(), usefind()to see if it appears anywhere in the string.Note also that I’ve modified the regex to only group the “My Error” part, instead of “: My Error” like your original one would have.
Just to be clear, here is the code I tested:
where
errorStringis the same as yours.The output is: