Community
    • Login

    Add something before a specific word with regex

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 2.8k 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.
    • Robin CruiseR
      Robin Cruise
      last edited by Robin Cruise

      Hi, Happy New Year !

      I have a problem at the end of the year. I must add a word before a specific word using regex.

        I have not bought in my life elsewhere than from us,</h1></td>
      

      So, i want to add the word SECRET before the word life , in order to become:

        I have not bought in my SECRET life elsewhere than from us,</h1></td>
      

      I tried a solution, but it’s not good.

      Search: (?:(life elsewhere)).*\G

      REPLACE BY: SECRET

      Of course, I can make a simple Search and Replace, but I need a regex solution.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Robin Cruise
        last edited by

        @Robin-Cruise ,

        it’s not good

        There are two reasons that’s not good. First, because you are consuming the life elsewhere text, and everything after that text, but not including any of that in the replacement. So instead of inserting, you are replacing. Second, because you are using the \G anchor, which anchors the end of your current match to the end of the previous match – and I don’t think you can back up to make the current match have to line up and end at the end of the previous (and for your stated goal, it’s completely unnecessary for doing something that complicated).

        There’s actually a third reason it’s not good, but that’s just from a stylistic view; this comment doesn’t affect whether or not it works. You are using the unnumbered-group construct (?:) around a normal numbered capture group (), which effectively says “avoid putting this group in a numbered group, but everything inside this group needs to go in a forced number group”. That would have been much easier as (life elsewhere) without the wrapping , still putting life elsewhere in group 1.

        But your stated goal doesn’t need any of those constructs. There are multiple ways I would accomplish this:

        1. FIND = life elsewhere, REPLACE = SECRET $0 – this just relies on the fact that the $0 substitution embeds the full match from FIND, so you are just putting SECRET followed by a space before it.
        2. FIND = (?=life elsewhere), REPLACE = SECRET\x20 – this one does a lookahead assertion, so life elsewhere must come immediately after the “cursor”, but none of your text gets “consumed” by the match; in the replacement, you just need SECRET and a space (which I indicated with \x20 so you could copy/paste, but you could just type the space after SECRET – either works), so it will put SECRET and space at the “cursor”, which is immediately before the matched-lookahead life elsewhere, and you don’t need to store any of the match portion, thus saving memory.
        3. FIND = life elsewhere, REPLACE = SECRET life elsewhere – this is the simplest, which will work whether or not you tick the regex box; but it is still a regex solution which meets all your example data, despite your artificial requirement of wanting a regex.

        Of course, I can make a simple Search and Replace, but I need a regex solution.

        Which could mean you actually aren’t telling us everything about your data, and you’re going to come back and change the rules on us when some uniqueness about your actual data contradicts what you’ve shown… Good luck with that.

        ----

        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 Markdown syntax. 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.

        1 Reply Last reply Reply Quote 2
        • Robin CruiseR
          Robin Cruise
          last edited by

          FIND: (.*)(life elsewhere)

          REPLACE BY: \1SECRET \2

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