regex search replace \1 not working



  • I am trying to search for a string followed by any number then replace the found string and number with two line feeds in front of it. The search works but the replace drops the found string. I have tried \0, \1, \2. This with NP++ 8.1.6. What am y

    Regular Expression
    Search: 3.[0-9]
    Replace: \n\n\1



  • @David-Klippel said in regex search replace \1 not working:

    3.[0-9]

    You are omitting the encoding of the capture operation.

    Try (3.[0-9])



  • @Neil-Schipper I knew I was missing something simple but was finding any clear examples. Thank you.



  • @David-Klippel

    It’s hard to tell, because you did not follow the Formatting Forum Posts link from the Please Read Before Posting. You also didn’t give any examples of before and after data, so that makes it hard to know if we’re correctly interpreting your question. The best-asked question follows the formatting advice, and gives both before and after data.

    Please note the expression as we can see it, 3.[0-9] searches for a literal 3, then any character (as indicated by regex .) and then any digit from 0-9. So it will match 3.0 but also 3p2 or 3 3 – I am not sure if that’s really what you’re trying to match.
    3bbe11ba-abde-4f05-bb02-091f8ffa98e9-image.png

    In your replacement, the \n will replace with just the LF character. If you are editing a file with Unix/Linux-style end-of-lines (EOL), that is correct; if you are editing a file with Windows EOL, then it’s only the LF half of the CRLF pair. In a windows file, a single EOF is indicated by \r\n in the replacement; so to get two newlines, you will want \r\n\r\n.

    The replacement \1 will insert the contents of the first match-group. Since you do not have any parentheses in your FIND expression, there are no matched groups, so nothing to replace. And, as documented in the regex substitutions section of the online user manual, \0 is not the right way to indicate the contents of the whole match; the correct syntax for that is $0 (or $& or $MATCH or …).

    With all of that, \r\n\r\n$0 is likely what you were intending for the replacement.

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual formatting commands.

    To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data.

    Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get.

    Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • @PeterJones said in regex search replace \1 not working:

    the \n will replace with just the LF character

    I wasn’t aware of that, and it’s good to know. I guess I assumed replace behaved like in some HLLs (printf in ‘C’, IIRC) which automagically terminate lines according to the conventions of the O/S.


Log in to reply