• Login
Community
  • Login

Replace text with \n\r using regular expression

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 3 Posters 7.3k 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.
  • S
    Stanisław Mędrzecki
    last edited by Mar 7, 2017, 11:18 AM

    Welcome everyone.

    At start I have to say, I’m not very advanced notepad ++ user. I know something about regular expressions, but I have to do something what is outside my knowledge.

    I need to replace specific string in every line for new line (<CR><LF>). The problem is that this specific string can be found more than one time in every line.

    Example:
    Original lines:
    ^WCONEX(905,1,“CONDICIONES INICIALES”,10)=TIR=-1#INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,30)=($E(TIR,2)=“O”)#RECIBE,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,40)=($E(TIR,2)=“R”)#RECIBE,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,50)=($E(TIR,1)=$C(5))#KILL,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,55)=($E(TIR,1)=$C(4))#KILL#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,60)=($L(TIR)>2)#ACK,INICIO#

    How should it looks like after replacement:
    ^WCONEX(905,1,“CONDICIONES INICIALES”,10)
    TIR=-1#INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,30)
    ($E(TIR,2)=“O”)#RECIBE,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,40)
    ($E(TIR,2)=“R”)#RECIBE,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,50)
    ($E(TIR,1)=$C(5))#KILL,ACK,INICIO#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,55)
    ($E(TIR,1)=$C(4))#KILL#
    ^WCONEX(905,1,“CONDICIONES INICIALES”,60)
    ($L(TIR)>2)#ACK,INICIO#

    As you can see only the first occurence of “=” was replaced with \r\n

    Is here any good soul that will help me and tell how the regular expression should look like?

    Regards for everyone

    C 1 Reply Last reply Mar 7, 2017, 3:56 PM Reply Quote 0
    • C
      Claudia Frank @Stanisław Mędrzecki
      last edited by Mar 7, 2017, 3:56 PM

      @Stanisław-Mędrzecki

      If I understand correctly you want to find everything until you match the first equal sign, correct?
      This means you need to search in non-greedy mode something like

      ^.*?=
      

      This would match anything from the start including the first equal sign
      See here for more details.
      When grouping your parts (before and after the equal sign) und can use the replace options

      \1\r\n\2
      

      to make it work. (\1 what has matched first, \2 second …)
      So I think you need to have

      (^.*?)=(.*$)
      

      for your example. (Note, ^ does not refer to the literal instead it is the anchor for start of line,
      whereas $ is end of line, of course only, if not dot matches newline is activated)

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • S
        Stanisław Mędrzecki
        last edited by Mar 7, 2017, 6:37 PM

        Yeah it looks like I need to find first equal sign and then everything what is on the left from “=” should stay in the same line, and everything on the right should be moved one line down, including removal of this first “=”.
        The main question is: Is it possible to do that in one expression?
        This files have sometimes more than 2000 lines before replacement and the process is sometimes very annoying.
        Now I do that in that way:
        Text to search: )=
        Text to replace: )\r\n

        Then I’m clicking on search, and if correct string is highlighted then replace.

        C 1 Reply Last reply Mar 7, 2017, 6:45 PM Reply Quote 0
        • C
          Claudia Frank @Stanisław Mędrzecki
          last edited by Claudia Frank Mar 7, 2017, 6:46 PM Mar 7, 2017, 6:45 PM

          @Stanisław-Mędrzecki

          seems my answer was to confusing, wasn’t it.

          Find: (^.*?)=(.*$)
          Replace: \1\r\n\2
          

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • S
            Stanisław Mędrzecki
            last edited by Mar 7, 2017, 6:57 PM

            @Claudia-Frank said:

            \1\r\n\2

            OMG! This works great. Thank you very much.

            1 Reply Last reply Reply Quote 0
            • R
              Robin Cruise
              last edited by Mar 8, 2017, 2:28 PM

              by the way, Claudia what actually does \1 and \2 ?

              C 1 Reply Last reply Mar 8, 2017, 3:10 PM Reply Quote 0
              • C
                Claudia Frank @Robin Cruise
                last edited by Claudia Frank Mar 8, 2017, 3:11 PM Mar 8, 2017, 3:10 PM

                @Robin-Cruise

                \1 and \2 refers to the matched grouped which is defined when using
                () in a regex.
                If you have for example

                (def)(.*)(\()
                

                and a text like

                def test_func()
                

                \1 = def
                \2 = test_func
                \3= (

                More on regex is here .

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 0
                • R
                  Robin Cruise
                  last edited by Mar 8, 2017, 4:09 PM

                  yes, I understand something now. But if I have something like this:

                  def test_func(.$)

                  \1 = def
                  \2 = test_func(.$)
                  \3= should be only (.*$) But doesn’t work.

                  C 1 Reply Last reply Mar 8, 2017, 4:27 PM Reply Quote 0
                  • C
                    Claudia Frank @Robin Cruise
                    last edited by Mar 8, 2017, 4:27 PM

                    @Robin-Cruise

                    so you have only one group, means \1 contains everything after test_tunc, unitl line end.

                    Cheers
                    Claudia

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