英文:
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 = <exception list...>
String regex = "\\w+Exception";
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<String> list = new ArrayList<>();
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论