简体   繁体   中英

How do I extract this string using a regular expression in Java?

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:

Pattern pattern = Pattern.compile("faultString: (.*)");

and then, instead of matcher.matches() , use find() 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:

Pattern pattern = Pattern.compile("faultString: (.*)");
Matcher matcher = pattern.matcher(errorString);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

where errorString is the same as yours.
The output is:

My Error

我可能会将Chris的正则表达式清理成以下内容: ".*faultString:\\\\s*([^\\\\n]*).*"

Pattern pattern = Pattern.compile("^faultString(.*)$", Pattern.MULTILINE);

This looks like property file format. Would it be easier to load this string into a java.util.Property using StringReader and then reading from it?

这适用于.matches()方法:

Pattern pattern = Pattern.compile(".*faultString([^\\n]*).*", Pattern.DOTALL);

Keep in mind that regex stuff is expensive. Chetan has the right idea.

Here's some sample code--

    String 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";

    Properties p = new Properties();
    ByteArrayInputStream bis = new ByteArrayInputStream(errorString
            .getBytes());

    try {
        p.load(bis);
    } catch (IOException e) {

    }

    System.out.println(p.toString());
    System.out.println(p.getProperty("faultString"));

Maybe getFaultString ? :)

edit: or ((AxisFault) exception.getRootCause()).getFaultString(). I just thought that you maybe overlooked the fact that you can get that directly from AxisFault itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM