如何使用Java中的正则表达式提取此字符串?

时间:2022-11-19 07:11:07
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.

我想得到“我的错误”,但它返回到整个字符串的末尾,而不是匹配到faultString行末尾的\ n。我已经尝试了许多技术让它在生产线结束时停止,但没有成功。

thanks

7 个解决方案

#1


You shouldn't be passing Pattern.DOTALL; that causes newlines to be matched by .*, which is exactly what you don't want here.

你不应该传递Pattern.DOTALL;这会导致换行符匹配。*,这正是你不想要的。

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.

然后,使用find()来查看它是否出现在字符串中的任何位置,而不是matcher.matches()。

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:

其中errorString与您的相同。输出是:

My Error

#2


I'd probably clean up Chris's regex to the following: ".*faultString:\\s*([^\\n]*).*"

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

#3


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

#4


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?

这看起来像属性文件格式。使用StringReader将此字符串加载到java.util.Property然后从中读取会更容易吗?

#5


This works with the .matches() approach:

这适用于.matches()方法:

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

#6


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

请记住,正则表达式的东西是昂贵的。 Chetan有正确的想法。

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

#7


Maybe getFaultString? :)

也许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.

编辑:或((AxisFault)exception.getRootCause())。getFaultString()。我只是觉得你可能忽略了这样一个事实,你可以直接从AxisFault本身那里得到它。

#1


You shouldn't be passing Pattern.DOTALL; that causes newlines to be matched by .*, which is exactly what you don't want here.

你不应该传递Pattern.DOTALL;这会导致换行符匹配。*,这正是你不想要的。

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.

然后,使用find()来查看它是否出现在字符串中的任何位置,而不是matcher.matches()。

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:

其中errorString与您的相同。输出是:

My Error

#2


I'd probably clean up Chris's regex to the following: ".*faultString:\\s*([^\\n]*).*"

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

#3


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

#4


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?

这看起来像属性文件格式。使用StringReader将此字符串加载到java.util.Property然后从中读取会更容易吗?

#5


This works with the .matches() approach:

这适用于.matches()方法:

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

#6


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

请记住,正则表达式的东西是昂贵的。 Chetan有正确的想法。

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

#7


Maybe getFaultString? :)

也许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.

编辑:或((AxisFault)exception.getRootCause())。getFaultString()。我只是觉得你可能忽略了这样一个事实,你可以直接从AxisFault本身那里得到它。