简体   繁体   English

notepad++ 正则表达式搜索失败

[英]notepad++ regular expression search failed

I try using the following to search some text and replace it by some other text starts with #.我尝试使用以下内容搜索一些文本并将其替换为以#开头的一些其他文本。 On regex101 it works fine but NotePad++ says search failed.在 regex101 上它工作正常,但 NotePad++ 说搜索失败。 Also tried to add \r before \n but didn't help.还尝试在 \n 之前添加 \r 但没有帮助。

regex: (class boy(?:.*\n)+\s*)(Height.*;\n)((?:.*\n)+})正则表达式: (class boy(?:.*\n)+\s*)(Height.*;\n)((?:.*\n)+})

Text to search:要搜索的文本:

class boy Mike
{
Age = 20;
PhoneNum = 658965;
Height = 198;
City = LA
}
note boy Joe
{
Age = 21;
PhoneNum = 558565;
Height = 178;
City = BA
}

class boy Joe
{
Age = 21;
PhoneNum = 558565;
Height = 178;
City = BA
}

Substitute: $1#$2$3替代品: $1#$2$3

Output: Output:

class boy Mike
{
Age = 20;
PhoneNum = 658965;
#Height = 198;
City = LA
}
note boy Joe
{
Age = 21;
PhoneNum = 558565;
Height = 178;
City = BA
}

class boy Joe
{
Age = 21;
PhoneNum = 558565;
#Height = 178;
City = BA
}

You can use您可以使用

Find What : ^class boy.*(?:\R(?.}$)?*)*\R\K(?=Height)查找内容^class boy.*(?:\R(?.}$)?*)*\R\K(?=Height)
Replace With : #替换为#
. matches newline : DISABLED匹配换行符:禁用

在此处输入图像描述

Details :详情

  • ^ - start of line ^ - 行首
  • class boy - class boy and then any zero or more chars as few as possible class boy - class boy ,然后尽可能少的任何零个或多个字符
  • .* - the rest of line .* - 线的rest
  • (?:\R(?.}$).*)* - zero or more lines up to end of file or to a line that is equal to } (?:\R(?.}$).*)* - 零或多行到文件末尾或等于}的行
  • \R - a line break sequence \R - 换行序列
  • \K - omit the text matched \K - 省略匹配的文本
  • (?=Height) - immediately on the right, there must be Height . (?=Height) - 就在右边,必须有Height

I have rewritten you regex so it doesn't use newline :我已经重写了你的正则表达式,所以它不使用newline

(?<=class boy \w+\s*\{[^}]+?)(Height[^;]+;)(?=[\s\S]+?\})

Use ' global ' and ' multiline ' options.使用“全局”和“多行”选项。

Replace with:用。。。来代替:

#$1

Explanation :说明

(?<=class boy \w+\s*\{[^}]+?) look behind for: class boy , a space and one or more word characters followed by one ore more white space (that includes newline). (?<=class boy \w+\s*\{[^}]+?)寻找: class boy ,一个空格和一个或多个单词字符,后跟一个或多个空格(包括换行符)。 Then look for { followed by one or more characters not being } .然后寻找{后跟一个或多个不是}的字符。

(Height[^;]+;) match 'Heigth' followed by one or more character not being ; (Height[^;]+;)匹配 'Heigth' 后跟一个或多个字符不是; - put the match in group 1. - 将比赛放在第 1 组。

(?=[\s\S]+?\}) look forward for any character followed by } . (?=[\s\S]+?\})期待任何后跟}的字符。

Replace with # and group 1.替换为#和组 1。

Another alternative...另一种选择...

Find;寻找; (class[\s\S]+?(?!note))(height)

Replace all: $1#$2$3全部替换: $1#$2$3

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM