Community
    • Login

    Replace a specific char with the begining of the line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 1.4k 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.
    • Axel BlitzA
      Axel Blitz
      last edited by PeterJones

      Hi friends, I hope you can help me please

      i have a text file formatted like this :

      Cell1-value1++value2++value3++value4…
      Cell2-valuea++valueb++valuec++valued…
      …
      Cell1213…
      Cell1214…

      What I want to do is to insert the Cell number between the double +

      Cell1-value1+Cell1+value2+Cell1+value3+Cell1+value4…
      Cell2-valuea+Cell2+valueb+Cell2+valuec+Cell2+valued…

      I tried with regex but didn’t find out how to solve this.

      Thank you for your help

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Axel Blitz
        last edited by PeterJones

        @Axel-Blitz said in Replace a specific char with the begining of the line:

        Cell1-value1++value2++value3++value4…
        Cell2-valuea++valueb++valuec++valued…

        • FIND = ^(\w+).*?\K\+\+
        • REPLACE = +$1+
        • SEARCH MODE = Regular Expression
        • REPLACE ALL

        Will replace the first ++ on each line with the cell value. Clicking Replace All N times (where N is the maximum number of ++ on any of the thousands of lines in your file – or just stop clicking when the Replace dialog says “0 occurrences were replaced in entire file”) will propagate it to the very end.

        Quick explanation:

        • ^ = start match at beginning of line
        • (\w+) = grab as many word characters (A-Z, 0-9, _) and store in group#1
        • .*? = move forward on the line until whatever comes next in the regex is matched (this is a way to skip 0 or more characters, when you don’t know how many characters it will be)
        • \K = this is a fancy thing that says “do all the matching before this symbol, but don’t include the text before this when i do a replacement later” – or, essentially, “keep (hence the \K) the text that comes before this”
        • \+\+ = match two literal + signs (+ is special to regex, so need a \ before it)
        • REPLACE: +$1+ => replace just the two + from the match with a single + then the contents of group 1 then a single + .

        (Sorry that you might have to hit Replace All multiple times. it might be possible to do with a very complicated single regular expression even if we cannot guarantee how many ++ there are beforehand, but it’s much easier to explain one where you just replace one ++ per line at a time. With a file with 1000 or 10000 lines, if you only have 10 ++ per line, so it grows with the number of ++ not with the number of lines, so there is at least that. And honestly, if you had more than 10 in a given row, the regex would be too complicated to type and use, so hitting 10 Replace All seems doable. If you might have 100 ++ on a single line, then I suggest recording the replacement I showed as a macro, then use Macro > Run a Macro Multiple Times to specify that you want to run the replacement 100 times without having to do 100 clicks.)

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        Axel BlitzA 1 Reply Last reply Reply Quote 1
        • Axel BlitzA
          Axel Blitz @PeterJones
          last edited by Axel Blitz

          @PeterJones said in Replace a specific char with the begining of the line:

          Peter Jones you saved my day !!
          Thank you very much <3

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