考虑一个文本文件中包含以下标题的表格
Table name goes here
Page 1
This is column one This is This
This is column is column
column two f thre f three f
and hal f
Row1 in column 1 Row2InCol2 Row3 Row4InCol4
Page 2
This is column one This is This
This is column is column
column two f thre f three f
and hal f
Grand Total: - 12 13 25
我想搜索列“This is column three f and a hal f”,这样当我找到这个文本时,我能够得到这个列开始的字符串索引位置(索引“This ") 和该列结束的索引位置(单词“half f”结束的索引,即“f”的索引)。请注意,所有列都包含单词“This”和字母“f”,并且我应该能够以与上述类似的方式搜索任何列的开始索引和结束索引。
我希望能够做到这一点,因为我想实现一个解析器,该解析器可以解析文本文件中的表格,其中列标题和列数据的索引位置从一页到另一页不一致(其中换页字符表示页面结束)
我不是在寻找任何算法。我想知道 Pattern 和 Matcher 类(或任何其他 API)是否支持如上所述的多行文本搜索?
最佳答案
过去对我有用的一个简单模式。
// split on two ore more spaces.
String[] fields = line.split("\\s{2,}");
这将一个空格视为字段的一部分。
关于java - 如何在 Java 中搜索多行并行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512590/