Home > Mobile >  notepad regular expression search failed
notepad regular expression search failed

Time:12-27

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
}

CodePudding user response:

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.

CodePudding user response:

Another alternative ...

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

Replace all: $1#$2

enter image description here

CodePudding user response:

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