notepad++ regular expression search failed

Viewed 284

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. Also tried to add \r before \n but didn't help.

regex: (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

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
}
3 Answers

You can use

Find What: ^class boy.*(?:\R(?!}$).*)*\R\K(?=Height)
Replace With: #
. matches newline: DISABLED

enter image description here

Details:

  • ^ - start of line
  • class boy - class boy and then any zero or more chars as few as possible
  • .* - the rest of line
  • (?:\R(?!}$).*)* - zero or more lines up to end of file or to a line that is equal to }
  • \R - a line break sequence
  • \K - omit the text matched
  • (?=Height) - immediately on the right, there must be Height.

Another alternative ...

Find;^(class[\s\S]+?)(height)

Replace all: $1#$2

enter image description here

I have rewritten you regex so it doesn't use 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). Then look for { followed by one or more characters not being }.

(Height[^;]+;) match 'Heigth' followed by one or more character not being ; - put the match in group 1.

(?=[\s\S]+?\}) look forward for any character followed by }.

Replace with # and group 1.

Related