Regex multi-line group substitution; Am I doing this wrong? Or is this a known issue/bug?



  • Moved this to the appropriate place.

    Hello All,

    I was looking for documentation regarding the built in regular expression find and replace setting. Specifically, why it seemed like multi-line group substitution wasn’t working.

    I found this page but it doesn’t seem to apply:
    https://notepad-plus-plus.org/community/topic/12973/multiple-line-replacing-doesn-t-work/5

    Original content.

    <tr style="height: 16p;">
          <td class="xl24" style="width: 10pt;"></td>
          <td class="xl24" style="width: 150pt;"></td>
          <td class="xl24" style="width: 150pt;"></td>
    </tr>
    

    Find what: 150pt(.*[\r\n]{1,2}.*)150pt
    Replace with what: 180pt\1120pt

    Instead of:

    <tr style="height: 16p;">
        <td class="xl24" style="width: 10pt;"></td>
        <td class="xl24" style="width: 180pt;"></td>
        <td class="xl24" style="width: 120pt;"></td>
    </tr>
    

    I was getting:

    <tr style="height: 16p;">
         <td class="xl24" style="width: 10pt;"></td>
         <td class="xl24" style="width: 180pt120pt;"></td>
    </tr>
    

    Am I doing something wrong or is this a bug/known issue?

    V/R
    ScottC



  • @Master3See

    That might be the oddest way to write a line ending in a regex that I’ve ever seen ([\r\n]{1,2}) but your regexes as specified make the desired substitution for me when I tried it, so I’m not sure what is going wrong for you. :(

    Some quick hints:

    • you can replace the very verbose [\r\n]{1,2} with simply \R
    • you might want to use ${1} in your replacement part instead of \1 because of the numbers directly following. Not a problem in your case but if you happened to have a lot of capturing groups, is \1120 supposed to be interpreted as group #1 followed by 120 or group #11 followed by 20 or…you get the idea…)

    So if I were doing it, it might be:

    Find-what zone: 150pt(.*\R.*)150pt
    Replace-with zone: 180pt${1}120pt

    But again, for me, your original expressions created your desired text from your original text.



  • Thanks Scott,

    I think I discovered the issue. This happens when performing the search with “Backward direction” checked. It does indeed work fine with my expressions in the forward direction.

    Thank you for the tips. That does clean up the expressions quite a bit.

    V/R Scott



  • @Master3See

    In Regular Expression search mode, backwards searching is problematic, you have probably found a case that exemplifies that.

    Okay, so if you have Backward direction ticked, and you toggle the search mode radio button to Normal and then back to Regular expression you will see that the Backward direction checkbox becomes UNCHECKED and DISABLED, so that you can no longer search backwards in Regular expression mode. No amount of further changing the user-interface will cause the checkbox to be enabled when regex is the active mode.

    There’s a bug in 7.5.* where the checkbox is enabled when it shouldn’t be (allowing your search to happen). Do this to surely see the bug:

    • Exit Notepad++ while Regular expression search mode is active
    • Restart Notepad++
    • Invoke the FInd window, Regular expression search mode is active, and the Backward direction checkbox is ENABLED (the bug is that it should be disabled in this circumstance)

    There’s some debate about whether or not backward searching should be allowed in regex mode. Indeed the “find” part of your search seems to work right, it’s the replace part that has trouble. Maybe that’s the objection…hmmm. Further confusing (or maybe clarifying!–see last sentence of THIS paragraph) this issue is that the Find Previous editor-mode shortcut key (or pressing Shift+Enter when the Find window has focus!) has (always) allowed backward regex searches. Unless it is truly the backward replace that’s a problem, in which case maybe it all makes sense… :-D



  • BTW, an issue was created for what I was just talking describing:
    #3640 – Backward-direction checkbox enabled with Reg Exp search mode (at N++ startup)

    …but discussion on it kind of got tied up with this issue:
    #3533 – <<Find" (Find Previous) button is disabled for “Regular expression” search

    …and sadly, none of this has gotten any attention to this point.


Log in to reply