Community
    • Login

    regex search replace \1 not working

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 2.0k 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.
    • David KlippelD
      David Klippel
      last edited by

      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

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • Neil SchipperN
        Neil Schipper
        last edited by

        @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])

        David KlippelD 1 Reply Last reply Reply Quote 2
        • David KlippelD
          David Klippel @Neil Schipper
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @David Klippel
            last edited by PeterJones

            @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.

            Neil SchipperN 1 Reply Last reply Reply Quote 2
            • Neil SchipperN
              Neil Schipper @PeterJones
              last edited by

              @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.

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