Negative lookbehind regular expression not working on Notepad++
-
Hello, @dr-ramaanand and All,
Ah, I just having some spare time to answer your specific problem !
As I said in a previous post, you cannot use look-behinds as they do NOT support variable quantifiers ! That’s why I proposed a method with the
((SKIP)(*F)feature, which works properly.
In order to found out other methods, let’s begin with a simplified problem :
Starting with this INPUT text :
DEF ABCDEF ABC DEF ABCDEF XYZDEF ABCDEFThe following regex S/R :
FIND
(?-is)ABCDEF(*SKIP)(*F)|DEFREPLACE
12345Matches only the DEF string when NOT preceded with the
ABCstringAfter the replacement, we get this OUTPUT :
12345 ABCDEF ABC 12345 ABCDEF XYZ12345 ABCDEF-
If
DEFis preceded byABC, NO replacement occurs -
If
DEFis NOT preceded byABCit replaces the stringDEFby the string12345
Now, using the same INPUT text :
The following regex S/R :
FIND
(?-is)ABCDEF\K|(DEF)REPLACE
?{1}12345-
Detect an empty match, when the
DEFstring is preceded by theABCstring -
Detect some selected text, when the
DEFstring is NOT preceded by theABCstring
But, due to the group, in the final part of the regex and due to the conditional replacement, it would produce this OUTPUT :
12345 ABCDEF ABC 12345 ABCDEF XYZ12345 ABCDEFAs previously :
-
if
DEFis preceded byABC, NO replacement occurs -
if
DEFis NOT preceded byABCit replaces the stringDEFby the string12345
Let’s apply this new regex S/R to your problem. Thus, from this regex :
FIND
(?:<span\b[^>]*?color\s*:\s*black[^>]*>\s*|<p\b[^>]*?color\s*:\s*black[^>]*>\s*<span\b[^>]*>\s*)<code\s*style="background-color:\s*transparent;">(*SKIP)(*F)|<code\s*style="background-color:\s*transparent;">If we simply replace the
(*SKIP)(*F)part by\KAND if we put all the right branch of the alternative within a group, this S/R becomes as :FIND
(?:<span\b[^>]*?color\s*:\s*black[^>]*>\s*|<p\b[^>]*?color\s*:\s*black[^>]*>\s*<span\b[^>]*>\s*)<code\s*style="background-color:\s*transparent;">\K|(<code\s*style="background-color:\s*transparent;">)REPLACE
?1[REPLACED text]When just searching, the non-interesting matches will be notified as an empty match and the correct matches will be notified as selected text.
And if a global replacement occur, with the
Replace Allbutton, this regex would just replace the same occurrences as with the(*SKIP)(*F)method !Try against your INPUT text, pasted in a new tab : you should get
2empty matchs and4selected range of characters. And, after replacement you’ll left with four zones[REPLACED text].<html> <p style="font-family: "verdana"; font-size: 18px; color: black; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: cyan;"><span style="font-size: 13.5pt; font-family: "Verdana","sans-serif";"><code style="background-color: transparent;">•••••<b>some text here</b></code></span></p> <span><span style="font-size: 13.5pt; font-family: "Verdana","sans-serif"; background-color: cyan;"><code style="background-color: transparent;"><b>some text here</b></code></span> <code style="background-color: transparent;"> <p style="font-family: "verdana"; font-size: 18px; color: cyan; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: cyan;"><span style="color: black; font-size: 13.5pt; font-family: "Verdana","sans-serif";"><code style="background-color: transparent;">•••••<b>some text here</b></code></span></p> <span><span style="font-size: 13.5pt; font-family: "Verdana","sans-serif"; background-color: cyan;"><code style="background-color: transparent;"><b>some text here</b></code></span> <p style="font-family: "verdana"; font-size: 18px; color: cyan; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: navy;"><span style="font-size: 13.5pt; font-family: "Verdana","sans-serif";"><code style="background-color: transparent;"><b>some text here</b></code></span></p> </html>
Now, it’s not the end of the story ! Imagine that, against the same INPUT text, below :
DEF ABCDEF ABC DEF ABCDEF XYZDEF ABCDEFWe use this simplified regex S/R :
FIND
(?-is)ABC\KDEF|(DEF)REPLACE
?{1}12345Note that, this time, the
\Kis placed before the stringDEFSo, it would match any string
DEF, whatever it’s preceded or NOT by theABCstringBut, due to the group, in the final part of the regex and due to the conditional replacement, We would get this new OUTPUT text, after a click on the
Replace Allbutton12345 ABC ABC 12345 ABC XYZ12345 ABC-
if
DEFis preceded byABC, it replaces the stringABCDEFby the stringABC -
if
DEFis NOT preceded byABCit replaces the stringDEFby the string12345
This S/R is a variant of the previous one which may interest you in some cases !
In short, I cannot imagine, presently, other methods than the two above, with
(SKIP)(*F)or\KRemember, that the look-arounds structure does not seem appropriate to your style of search !
Best Regards,
guy038
-
-
@guy038 Oui, merci beaucoup!
-
This post is deleted!