import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\\"error_description\\\"\\:\\\"(?P<error_description>.\\w+\\s+\\w+)";
final String string = "\"error\":\"invalid_grant\",\"error_description\":\"authentication failure\"}\"\n"
+ "[WARN ] 2016-06-15 17:55:03,835 ajp-bio-8009-exec-147: failed to unmarshall the body of the response:\n"
+ "{\"error\":\"invalid_grant\",\"error_description\":\"authentication failure\"}\n"
+ "javax.xml.bind.UnmarshalException\n"
+ " - with linked exception:\n"
+ "[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)\n"
+ " at com.cisco.hn.cs.common.http.client.RestClientErrorHandler.handleError(RestClientErrorHandler.java:41)\n"
+ " at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)\n"
+ " at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)\n"
+ " at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)\n"
+ " at org.springframework.web.client.Res[WARN ] 2016-06-15 17:55:03,835 ajp-bio-8009-exec-147: failed to unmarshall the body of the response:\n"
+ "{\"error\":\"invalid_grant\",\"error_description\":\"authentication failure\"}\n"
+ "javax.xml.bind.UnmarshalException\n"
+ " - with linked exception:\n"
+ "[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)\n"
+ " at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)\n"
+ " at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)\n"
+ " at com.cisco.hn.cs.common.http.client.RestClientErrorHandler.handleError(RestClientErrorHandler.java:41)\n"
+ " at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)\n"
+ " at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)\n"
+ " at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)\n"
+ " at org.springframework.web.client.Res";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html