我有一个字符串数组列表,我想找到
//results.getOptions() is an ArrayList<String>
Integer counter = 0;
for ( String option : results.getOptions() ) {
System.err.println( "Item "+counter+" :"+option );
counter++;
}
这段代码的输出是:
Item 0 :<ET>read input: 11.844ms</ET>
Item 1 :<ET>import: 2069.9ms</ET>
Item 2 :<ET>calc: 23.022ms</ET>
Item 3 :<ET>decompress .tax: 5.451ms</ET>
Item 4 :<ET>decrypt .tax: 4.409ms</ET>
Item 5 :<ET>load .tax formsets: 7.929ms</ET>
Item 6 :<ET>There were 4 calc errors:
Item 7 :F941 0 ZIP 0 - <Error><FormCd>INWKS941</FormCd><Level>Fatal</Level><Source>Company</Source><Entity>50-7754170</Entity><Category>CompanyInfo</Category><Message>Zip code is invalid. You must enter a valid ZIP code for your state. Enter a correct ZIP code in this format 'nnnnn' or 'nnnnn-nnnn'.</Message></Error>.
Item 8 :F941 0 STATE 0 - <Error><FormCd>INWKS941</FormCd><Level>Fatal</Level><Source>Company</Source><Entity>50-7754170</Entity><Category>CompanyInfo</Category><Message>State abbreviation is invalid. Enter your two-letter postal state abbreviation.</Message></Error>.
Item 9 :F941 0 L11 0 - <Error><FormCd>INWKS941</FormCd><Level>Fatal</Level><Source>Company</Source><Entity>50-7754170</Entity><Category>Calculation</Category><Message>Total taxes after adjustments does not equal the total quarter liability on Schedule B. You must make the necessary adjustments to reconcile the amounts.</Message></Error>.
Item 10 :F941 0 L15 0 - <Error><FormCd>INWKS941</FormCd><Level>Informational</Level><Source>Company</Source><Entity>50-7754170</Entity><Category>FormInfo</Category><Message>There is a balance due on this form of $6567.78.</Message></Error>.
Item 11 :: 0.034ms</ET>
Item 12 :<ET>write FormML: 8.739ms</ET>
Item 13 :<ET>flush FormML: 0.602ms</ET>
Item 14 :<ET>copy FormML to output vector: 1.763ms</ET>
Item 15 :<ET>convert: 2147.71ms</ET>
Item 16 :<ET>write output: 0.782ms</ET>
Item 17 :<FORMSET id="FORMML"/>
Item 18 :<DATA size="247750"/>
Item 19 :<ERROR code="0"/>
Item 20 :
我想捕获以下开头的文本行(索引):
<ET>There were 4 calc errors:
最后是:
</ET>
(输出中的第 6 - 11 项)
捕获这些特定行的正则表达式是什么?我有一段 Java 代码,它将返回索引,但是捕获这些行的正则表达式是什么?
List<String> getMatchingStrings(List<String> list, String regex) {
ArrayList<String> matches = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
for (String s:list) {
if (p.matcher(s).matches()) {
matches.add(s);
}
}
return matches
}
最佳答案
首先,正则表达式对单个字符串进行操作,因此您必须组合单独的字符串。您可能希望将它们视为线条,所以这会起作用。
StringBuilder buf = new StringBuilder();
for (String option : results.getOptions()) {
buf.append(option).append("\r\n");
}
接下来,您需要一个跨多行工作的正则表达式,因此您需要 DOTALL
选项(“在 dotall 模式下,表达式 . 匹配任何字符,包括行终止符”)。
此外,您需要正则表达式“不情愿”,与“贪婪”相反,因此您需要 .*?
,而不是 .*
,并且您想要捕获开始模式和结束模式之间的文本,因此您需要一个捕获组 ()
。
String regex = "<ET>There were 4 calc errors:(.*?)</ET>";
Pattern p = Pattern.compile(regex, Pattern.DOTALL);
Matcher m = p.matcher(buf.toString());
while (m.find()) {
String errorText = m.group(1);
// Use errorText here
}
当然,以防万一没有恰好 4 个错误,您可以使用此:
String regex = "<ET>There were \\d+ calc errors:(.*?)</ET>";
捕获的文本将以换行符开头,因此您可以trim()
错误文本,或添加到模式中。
String regex = "<ET>There were \\d+ calc errors:\r\n(.*?)</ET>";
关于java - 用于在 ArrayList 中查找特定字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32131404/