java - 如何在 Java 中搜索多行并行文本?

标签 java regex pattern-matching

考虑一个文本文件中包含以下标题的表格

    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/

相关文章:

正则表达式:分别捕获名称和部门

function - OCaml:迭代列表并跳过错误构造函数的元素

java - 如何在 spring xml 文件上启用 ehcache 统计信息

java - 如何在 Spring 类 AuthenticationProvider 中拦截 403 http 状态时来自 REST API 的响应

javascript - 正则表达式 javascript 仅返回一个值而不是完全匹配

Python 正则表达式列表使用列表

Scala 可变集合和 "Reference must be prefixed warnings"

java - 不带空格的文本搜索

java - jvisualvm 去哪儿了?

java - BaseButton<E> 和 BaseButton<E extends BaseButton<E>> 有什么区别?