【发布时间】:2015-05-03 10:21:44
【问题描述】:
考虑一个由N lines 组成的多行字符串,如下所示:
Line 1 text
Line 2 text
Line 3 text
...
Line n-1 text
Line n text
anchor=value
Line n+2 text
Line n+3 text
Line n+4 text
...
Line N text
anchor 键不会出现在任何行内,并且在锚点之前以及其后的 = 符号周围可能有空格。
我需要一个将上述字符串分成 3 组的正则表达式:
- 第 1 行至第 n 行(含)
- 锚线(分割点)
- 第 n+2 行至第 N 行(含)
我最接近解决方案的是
(?s)^(?:(?!anchor\s*=\s*).)+?\r|\nanchor\s*=\s*([^\r\n]+)(?:\r|\n)(.*)
但上述正则表达式将整个文本包含在第一个匹配组中,并按预期填充剩余的 2 个组。
另一个要求是正则表达式必须尽可能快,因为它将应用于大量数据。另请注意,在此用例中,通过单个正则表达式进行处理是唯一的选择。
有什么想法吗?
【问题讨论】:
标签: java regex multiline multilinestring