I want to extract all substring that match regex pattern from a String and store them in a String array

huangapple go评论74阅读模式
英文:

I want to extract all substring that match regex pattern from a String and store them in a String array

问题

我想提取所有包含异常的子字符串。例如,从下面的字符串中提取"SQLException"、"SQLSyntaxErrorException"等,并将这些值存储在一个字符串数组中。我该如何操作?我尝试使用split(regex)方法,但它只会将其他所有内容都存储在数组中,而不是那些异常。帮助是可以的。

public static void exceptionsOutOfString(){
    String input = "org.hibernate.exception.SQLException: error executing work org.hibernate.exception.SQLGrammarException: error \n" +
            "executing work     at  ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     \n" +
            "\\norg.hibernate.exception.SQLGrammarException: error executing work     at  ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] \n" +
            "     Caused by: java.sql.SQLSyntaxErrorException: malformed string: 'Acme'    at";

    String regex = "\\.([a-zA-Z]*Exception)";

    String[] exceptions  = input.split(regex);
英文:

I want to extract all the substrings that have Exceptions. For example, "SQLException", "SQLSyntaxErrorException" etc from the below string, and store those values in a String array. How do I go about it? I tried to use the split(regex) method but that stores everything else in the array but not those exceptions. Help is appreciated.

public static void exceptionsOutOfString(){
    String input = "org.hibernate.exception.SQLException: error executing work org.hibernate.exception.SQLGrammarException: error \n" +
            "executing work     at  ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     \n" +
            "\\norg.hibernate.exception.SQLGrammarException: error executing work     at  ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] \n" +
            "     Caused by: java.sql.SQLSyntaxErrorException: malformed string: 'Acme''    at";

    String regex = ".[a-zA-Z]*Exception";

    String[] exceptions  = input.split(regex);

答案1

得分: 1

试试这个:

String input = <exception list...>;
String regex = "\\w+Exception";
Matcher m = Pattern.compile(regex).matcher(input);

while (m.find()) {
    System.out.println(m.group());
}

输出结果

SQLException
SQLGrammarException
SQLGrammarException
SQLSyntaxErrorException

要将它们放入一个数组中,可以按照以下步骤操作:

String[] array = m.results()
             .map(MatchResult::group)
             .toArray(String[]::new);

m.results() 生成一个 MatchResults 的流。因此,获取该流并使用 group 方法获取字符串,然后返回一个数组。

正如 Abra 所观察到的那样,上述方法直到 JDK 9 才被引入。

以下是另一种方法。

List<String> list = new ArrayList<>();
while (m.find()) {
     list.add(m.group());
}

然后可以将其作为列表使用或进行转换。

String[] array = list.stream().toArray(String[]::new);
// 或者
String[] array = list.toArray(String[]::new); // JDK 11

注意:这只是翻译,不包含其他内容。

英文:

Try this:

String input = &lt;exception list...&gt;
String regex = &quot;\\w+Exception&quot;;
Matcher m = Pattern.compile(regex).matcher(input);

while (m.find()) {
	System.out.println(m.group());
}

Prints

SQLException
SQLGrammarException
SQLGrammarException
SQLSyntaxErrorException

To put them in an array, do the following:

String[] array = m.results()
             .map(MatchResult::group)
             .toArray(String[]::new);

m.results() produces a stream of MatchResults. So take that and use the group method to get the string then return an array.

As was astutely observed by Abra, the above was not introduced until release JDK 9.

Here is an alternative.

List&lt;String&gt; list = new ArrayList&lt;&gt;();
while (m.find()) {
	 list.add(m.group());
}

Then either use as a list or convert.

String[] array = list.stream().toArray(String[]::new);
// or
String[] array = list.toArray(String[]::new); // JDK 11

huangapple
  • 本文由 发表于 2020年7月25日 06:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63082311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定