查找搜索项目前后加4行

时间:2014-06-16 07:14:12

标签: regex notepad++ regex-lookarounds

我正在使用notepad++,并希望找到特定字符串出现的上下文。

所以搜索字符串是0wh.*0subj,我希望在它之前和之后找到这个搜索项加上4行。

eg: xxx means whatever is on a new line. the search result should be:
xxx
xxx
xxx
xxx
0wh.*0subj
xxx
xxx
xxx
xxx

我已经尝试了using \n\r但它没有用。我们将非常感谢所提供的任何帮助。

此致

1 个答案:

答案 0 :(得分:5)

这适用于Notepad ++(已测试):

(?m)(^[^\r\n]*\R+){4}0wh\.\*0subj[^\r\n]*\R+(^[^\r\n]*\R+){4}

在屏幕截图中,请注意未选择555行。这只是当前的一行。

SO N++

解释正则表达式

(?m)                     # set flags for this block (with ^ and $
                         # matching start and end of line) (case-
                         # sensitive) (with . not matching \n)
                         # (matching whitespace and # normally)
(                        # group and capture to \1 (4 times):
  ^                      #   the beginning of a "line"
  [^\r\n]*               #   any character except: '\r' (carriage
                         #   return), '\n' (newline) (0 or more times
                         #   (matching the most amount possible))
  \R+                    #   'R' (1 or more times (matching the most
                         #   amount possible))
){4}                     # end of \1 (NOTE: because you are using a
                         # quantifier on this capture, only the LAST
                         # repetition of the captured pattern will be
                         # stored in \1)
0wh                      # '0wh'
\.                       # '.'
\*                       # '*'
0subj                    # '0subj'
[^\r\n]*                 # any character except: '\r' (carriage
                         # return), '\n' (newline) (0 or more times
                         # (matching the most amount possible))
\R+                      # 'R' (1 or more times (matching the most
                         # amount possible))
(                        # group and capture to \2 (4 times):
  ^                      #   the beginning of a "line"
  [^\r\n]*               #   any character except: '\r' (carriage
                         #   return), '\n' (newline) (0 or more times
                         #   (matching the most amount possible))
  \R+                    #   'R' (1 or more times (matching the most
                         #   amount possible))
){4}                     # end of \2 (NOTE: because you are using a
                         # quantifier on this capture, only the LAST
                         # repetition of the captured pattern will be
                         # stored in \2)