查找搜索项以及之前和之后的4行 [英] find search item plus 4 lines before and after

查看:126
本文介绍了查找搜索项以及之前和之后的4行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用notepad++,并且想查找出现特定字符串的上下文.

I am using notepad++ and would like to find the context in which a particular string occurs.

因此搜索字符串为0wh.*0subj,我想找到此搜索项以及紧接在它之前和之后的4行.

So the search string is 0wh.*0subj and I would like to find this search item plus 4 lines immediately before and after it.

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,但是它不起作用.任何提供的帮助将不胜感激.

I have tried using \n\r but its not working. Any assistance afforded would be greatly appreciated.

致谢

推荐答案

这将在Notepad ++中运行(已测试):

This will work in Notepad++ (tested):

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

在屏幕截图上,请注意未选择555行.这只是当前行.

On the screenshot, note that the 555 line is not selected. It is just the current line.

解释正则表达式

(?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)

这篇关于查找搜索项以及之前和之后的4行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆