Community
    • 登入

    Replace text with \n\r using regular expression

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    9 貼文 3 Posters 7.5k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Stanisław MędrzeckiS
      Stanisław Mędrzecki
      最後由 編輯

      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 FrankC 1 條回覆 最後回覆 回覆 引用 0
      • Claudia FrankC
        Claudia Frank @Stanisław Mędrzecki
        最後由 編輯

        @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 條回覆 最後回覆 回覆 引用 0
        • Stanisław MędrzeckiS
          Stanisław Mędrzecki
          最後由 編輯

          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 FrankC 1 條回覆 最後回覆 回覆 引用 0
          • Claudia FrankC
            Claudia Frank @Stanisław Mędrzecki
            最後由 Claudia Frank 編輯

            @Stanisław-Mędrzecki

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

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

            Cheers
            Claudia

            1 條回覆 最後回覆 回覆 引用 0
            • Stanisław MędrzeckiS
              Stanisław Mędrzecki
              最後由 編輯

              @Claudia-Frank said:

              \1\r\n\2

              OMG! This works great. Thank you very much.

              1 條回覆 最後回覆 回覆 引用 0
              • Robin CruiseR
                Robin Cruise
                最後由 編輯

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

                Claudia FrankC 1 條回覆 最後回覆 回覆 引用 0
                • Claudia FrankC
                  Claudia Frank @Robin Cruise
                  最後由 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 條回覆 最後回覆 回覆 引用 0
                  • Robin CruiseR
                    Robin Cruise
                    最後由 編輯

                    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 FrankC 1 條回覆 最後回覆 回覆 引用 0
                    • Claudia FrankC
                      Claudia Frank @Robin Cruise
                      最後由 編輯

                      @Robin-Cruise

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

                      Cheers
                      Claudia

                      1 條回覆 最後回覆 回覆 引用 0
                      • 第一個貼文
                        最後的貼文
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors