gpt4 book ai didi

regex - 查找搜索项加上前后 4 行

转载 作者:行者123 更新时间:2023-12-02 00:37:27 25 4
gpt4 key购买 nike

我正在使用 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

我已经尝试使用\n\r 但它不起作用。任何提供的帮助将不胜感激。

问候

最佳答案

这将在 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)

关于regex - 查找搜索项加上前后 4 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24238348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com