Tip to simulate a BACKWARD regex replacement, without using the 'Backward direction' option
-
Hello, All,
From this post :
https://community.notepad-plus-plus.org/topic/19632/faq-regex-backtracking-control-verbs/1
Allow me to emphasize a specific point, regarding the
COMMIT
backtracking control verb, already discussed !
We all know, that, if the
Wrap around
option is not used, the search process is run from current location till the very end of file(s). Now, imagine that you would like to execute an regex S/R from current position till the very beginning of file. Well :-
The backward option is not enabled, by default when a regex search is involved
-
Even if you change the
regexBackward4PowerUser
attribute, from no to yes, in theFindHistory
section of theconfig.xml
file, some regexes do not behave properly in backward direction !
Now, let’s add a simple character, not used yet, alone in a new line, somewhere in your current file. So, it will divide the file contents in two parts :
-
a first part before this single character
-
A second part after this single character
I chose the
%
character but, of course, any other char not yet used, may be chosen ! Now, let’s see the behavior of the regex%(*COMMIT*)\w+|\w+
:-
As long as the
%
character is not found, at current starting position, the second alternative of the regex is used and matches any word (\w+
) -
As soon as the
%
character is found, at current starting position, the regex engine is committed to find some word chars, right after the%
symbol. This is impossible as it is followed with a line-break ! So, due to the(*COMMIT)
verb, the overall regex fails and the search process stops !
Thus, if we consider the shorter search regex
(%(*COMMIT)|)\w+
and, for example, the replacement regex\U$0
with theWrap around
option ticked, this S/R would change any word in uppercase in the part of text above the%
character only and the replacement process would stop as soon as a%
symbol, not immediately followed by a word char, is found, further on, in current text !
Example :
- Paste the text, below, in a new tab
This is a short sample of text to verify if the COMMIT regex % works as expected whatever the present caret position
-
Switch to this new file
-
Open the Replace dialog (
Ctrl + H
) -
SEARCH
(%(*COMMIT)|)\w+
-
REPLACE
\U$0
-
Do not tick the
Backward direction
option ( IMPORTANT ) -
Check the
Wrap around
option -
Select the
Regular expression
search mode -
Click on
Replace All
button
=> Whatever the present position of the caret, only the part of text, above the
%
character, is changed in uppercase letters !Second example :
-
Now, restore the file as before the replacement (
Ctrl + Z
) -
Change the search and replace zones as :
-
SEARCH
(%(*COMMIT)|)(?-s).+
-
REPLACE
// $0
-
-
Click again on the
Replace All
button
=> This time, whatever the present position of the caret, only the part of text, above the
%
character, is considered asC
comments !
Of course, I used very basic regexes but this trick may be interesting when dealing with a complicated regex structure, which could fail when the
Backward direction
option is used !Best Regards,
guy038
-