• Login
Community
  • Login

Tip to simulate a BACKWARD regex replacement, without using the 'Backward direction' option

Scheduled Pinned Locked Moved General Discussion
1 Posts 1 Posters 314 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G
    guy038
    last edited by guy038 Apr 5, 2024, 3:45 PM Apr 5, 2024, 12:04 PM

    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 the FindHistory section of the config.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 the Wrap 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 as C 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

    1 Reply Last reply Reply Quote 1
    1 out of 1
    • First post
      1/1
      Last post
    The Community of users of the Notepad++ text editor.
    Powered by NodeBB | Contributors