3

在任何编程语言中,我都知道如何有效地限制给定文件或字符串中的行数,这不是这里的问题。但是在这种情况下,我希望通过正则表达式来做到这一点。在这种模式中,我只使用\n 换行符。我不需要\r 回车之类的其他东西。

(?:(?:\n)?[^\n]*){0,3}

上面的正则表达式解释:

(?:       group, but do not capture (between 0 and 3 times)-
 (?:      group, but do not capture (optional)
  \n      '\n' (newline)
 )?       end of grouping
 [^\n]*   any character except: '\n' (newline) (0 or more times)
){0,3}    end of grouping

现在在字符串中使用这个正则表达式,例如..

In this line is foo bar and baz
In this line is bar and foo
In this line is baz and bar
In this line we have foo
In this line we have bar and foo and baz
In this line we have foobar
In this line we have foo
In this line we have foo and bar
In this line we have bar and baz and foo

这将返回1-3没有问题的行。

在上面的字符串中,lines 789all 包含单词fooall 本身,无论它是在字符串的开头、中间还是结尾。

现在我的问题是如何实现前瞻或后视来搜索字符串并3在一行中查找所有具有相同关键字的文本行,foo而不是作为单词的前缀或组合在另一个单词中?因此,它只匹配行7-9而不匹配1-6

4

1 回答 1

4

我不明白为什么这需要任何形式的环视。只匹配包含的行foo

(?:\n?[^\n]*foo[^\n]*){3}

请注意,使用可选的\nthis 可能会匹配包含foo3 次的行。为避免这种情况,请使用

(?:(?:^|\n)[^\n]*foo[^\n]*){3}
// or
(?:[^\n]*foo[^\n]*(?:\n|$)){3}

(根据您的正则表达式风格,您可能会使用不同的锚点作为字符串开始/结束)


如果您需要foo独立存在,只需为其添加单词边界

(?:\n?[^\n]*\bfoo\b[^\n]*){3}
于 2013-09-10T23:35:42.663 回答