Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Replace text with \n\r using regular expression

    Help wanted · · · – – – · · ·
    3
    9
    6281
    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.
    • Stanisław Mędrzecki
      Stanisław Mędrzecki last edited by

      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

      Claudia Frank 1 Reply Last reply Reply Quote 0
      • Claudia Frank
        Claudia Frank @Stanisław Mędrzecki last edited by

        @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
        • Stanisław Mędrzecki
          Stanisław Mędrzecki last edited by

          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.

          Claudia Frank 1 Reply Last reply Reply Quote 0
          • Claudia Frank
            Claudia Frank @Stanisław Mędrzecki last edited by Claudia Frank

            @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
            • Stanisław Mędrzecki
              Stanisław Mędrzecki last edited by

              @Claudia-Frank said:

              \1\r\n\2

              OMG! This works great. Thank you very much.

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

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

                Claudia Frank 1 Reply Last reply Reply Quote 0
                • Claudia Frank
                  Claudia Frank @Robin Cruise last edited by Claudia Frank

                  @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
                  • Robin Cruise
                    Robin Cruise last edited by

                    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.

                    Claudia Frank 1 Reply Last reply Reply Quote 0
                    • Claudia Frank
                      Claudia Frank @Robin Cruise last edited by

                      @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
                      • First post
                        Last post
                      Copyright © 2014 NodeBB Forums | Contributors