【发布时间】:2013-12-10 06:57:25
【问题描述】:
我有以下输入/输出和正则表达式代码可以正常工作(对于以下输入/输出)。
-- 输入--
keep this
keep this too
Bye
------ Remove Below ------
remove all of this
-- 输出--
keep this
keep this too
Bye
-- 代码--
String text = "keep this\n \n"
+ " keep this too\n \n Bye\n------ Remove Below ------\n remove all of this\n";
System.out.println(text);
Pattern PATTERN = Pattern.compile("^(.*?)(-+)(.*?)Remove Below(.*?)(-+)(.*?)$",
Pattern.DOTALL);
Matcher m = PATTERN.matcher(text);
if (m.find()) {
// remove everything as expected (from about input->regex->output)
text = ((m.group(1)).replaceAll("[\n]+$", "")).replaceAll("\\s+$", "");
System.out.println(m.group(1));
System.out.println(text);
}
好的,这很好用。但是,这是针对已定义输入输出的测试。当我得到我必须解析的包含以下字符/模式序列的大文件时,我看到对于大小为 100k 的文件,按照 Find() 方法执行代码需要一段时间(4-5 秒)有以下模式。事实上,有时我不确定它是否会返回......当我作为调试测试单步执行时,find() 方法挂起并且我的客户端断开连接。
注意:此文件中没有可匹配的内容...但这是对我的正则表达式征税的模式。
-- 100k 文件--
junk here
more junk here
o o o (even more junk per the ellipses)
-------------------------------------this is junk
junk here
more junk here
o o o (even more junk per the ellipses)
-------------------------------------this is junk
junk here
more junk here
o o o (even more junk per the ellipses)
-------------------------------------this is junk
junk here
more junk here
o o o (even more junk per the ellipses)
this repeats from above to make up the 100k file.
-- 提问--
如何优化上述正则表达式以处理来自 以上是这样还是正则表达式解析速度(4-6秒)完全挂起?
【问题讨论】: