Community
    • 登入

    replace one word/number with another word/number on the same line with regular expression

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    5 貼文 4 Posters 502 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Robin CruiseR
      Robin Cruise
      最後由 編輯

      hello, I need a little help, please. I have this 2 lines/rows that both contains the number 2002 and the word Michael. I want to replace 2002 with 2012 in each line that contains Michael.

      There was a literary and surrealistic magazine edited by André Breton. On March 02, 2002, in Performance Management, by Michael London I get the book.

      Its first issue was published on December 14, 2002, in Performance Management book, written by Michael London after his wife get him...

      I made a regex, it is almost good, but instead of replacing it, it adds 2012 near 2002. I don’t know what I did wrong. Can anyone help me?

      FIND: (^.*)(?=(2002))(.*?)(Michael)(.*?)
      REPLACE BY: \12012, \3\4\5

      Terry RT 1 條回覆 最後回覆 回覆 引用 1
      • Terry RT
        Terry R @Robin Cruise
        最後由 編輯

        @Robin-Cruise said in replace one word/number with another word/number on the same line with regular expression:

        (^.)(?=(2002))(.?)(Michael)(.*?)

        The way I read your regex it appears that although you think you are capturing 2002 in group 2, as it is inside the positive look ahead that prevents the regex engine actually moving the cursor forward. Then it starts on group 3 which AGAIN captures 2002, hence you write it back.

        Just remove the positive look ahead leaving it as (2002). I think that will solve your main issue. I do note that you write a comma back, yet I believe you already captured it so possible some other minor changes also required.

        I’m not on a PC to check this myself.

        Terry

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

          @Terry-R said in replace one word/number with another word/number on the same line with regular expression:

          (^.)(?=(2002))(.?)(Michael)(.*?)

          thanks

          should be:

          FIND: (^.*)(2002)(.*?)(Michael)(.*?)

          REPLACE BY: \12012\3\4\5

          Alan KilbornA 1 條回覆 最後回覆 回覆 引用 2
          • Alan KilbornA
            Alan Kilborn @Robin Cruise
            最後由 Alan Kilborn 編輯

            @Robin-Cruise

            Thanks for showing what you tried, and what you had success with.

            Your final regex works if 2002 appears BEFORE Michael on a line (or multiple lines as you don’t qualify your usage of .).

            This pair seems to correct those deficiencies:

            find: (?-s)^(?=.*?Michael)(.*?20)02
            repl: ${1}12

            1 條回覆 最後回覆 回覆 引用 2
            • guy038G
              guy038
              最後由 編輯

              Hello, @robin-cruise, @Terry-R and All,

              Why not this simplified version :

              SEARCH (?-is)2002(?=.*Michael)

              REPLACE 2012

              Notes :

              • The regex searches for the string 2002 ONLY IF  , further on, in current line, the string Michael, with that exact case, can be found

              • If so, it is replaced with the string 2012

              • In case, the forename Michael could be located before the 2002 string, here is an improved version taking the two cases in account :

                • SEARCH (?-is)2002(?=.*Michael)|Michael.*\K2002

                • REPLACE 2012

                • Use the Replace All button, only !

              Best Regards,

              guy038

              P.S. :

              Oh…, I’ve just seen the @alan-kilborn version which is really clever as it does not need an alternative ;-))

              So, I just added the (?-i) modifier, if necessary and the \K feature to avoid any group :

              • SEARCH (?-is)^(?=.*?Michael).*\K2002

              • REPLACE 2012

              • Use the Replace All button, only !

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