java在流上执行正则表达式
我有一些大的文本文件,我将对它们进行连续匹配(只是捕获,而不是替换)。我认为将整个文件保存在内存中不是一个好主意,而是使用Reader
我所知道的输入是,如果有匹配,它不会跨越超过5行。所以我的想法是有一个缓冲区,只保留这5行左右,进行第一次搜索,然后继续。但它必须“知道”正则表达式匹配在哪里结束,这样才能起作用。e、 g如果匹配在第2行结束,则应从此处开始下一次搜索。有没有可能以一种有效的方式来做这样的事情
你可以在下面搜索框中键入要查询的问题!
我有一些大的文本文件,我将对它们进行连续匹配(只是捕获,而不是替换)。我认为将整个文件保存在内存中不是一个好主意,而是使用Reader
我所知道的输入是,如果有匹配,它不会跨越超过5行。所以我的想法是有一个缓冲区,只保留这5行左右,进行第一次搜索,然后继续。但它必须“知道”正则表达式匹配在哪里结束,这样才能起作用。e、 g如果匹配在第2行结束,则应从此处开始下一次搜索。有没有可能以一种有效的方式来做这样的事情
# 1 楼答案
也许
Scanner.matchAll()
就是你想要的。它简化了我的代码# 2 楼答案
您可以使用} 方法:
Scanner
和^{从^{} 上的api:
附带说明:当在多行上进行匹配时,您可能希望查看常量
Pattern.MULTILINE
和Pattern.DOTALL
# 3 楼答案
Streamflyer能够在字符流上应用正则表达式
请注意,我是这本书的作者
# 4 楼答案
正则表达式引擎的java实现看起来不适合流式处理
我宁愿提倡另一种基于“导数组合”的方法
研究人员Matt May在他的博客上发表了关于“导数组合子”的相关帖子,并建议在这里使用Scala实现:
就我而言,我通过添加一些“捕获”功能成功地改进了这个实现,但我觉得它可能会对内存消耗产生重大影响
# 5 楼答案
# 6 楼答案
使用Java8,您可以非常简单地并行地完成这项工作-
另一种方法可以是——