• Login
Community
  • Login

Replace differing text with one text

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 3 Posters 13.7k 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
    Savey Traveller
    last edited by Sep 20, 2017, 10:38 PM

    Hi

    I have a lot of files where the text is like this

    “abcdefg” that part is constant, then the next part could be anything and I want to replace the second part with a single text.

    For example
    “Tomorrow Situps”
    “Tomorrow Stand”
    “Tomorrow Run”

    I need to replace the text after Tomorrow with Kneel.

    Is this possible

    Thanks

    savey

    1 Reply Last reply Reply Quote 0
    • M
      Meta Chuh moderator
      last edited by Sep 20, 2017, 10:54 PM

      @Savey-Traveller

      to replace an unknown word after a known word (constant) with a single text:

      original text:

      "Tomorrow Situps"
      "Tomorrow Stand"
      "Tomorrow Run"
      

      notepad++ replace, search mode: regular expression
      find what:

      "Tomorrow (.*?)"
      

      replace with:

      "Tomorrow Kneel"
      

      then click on replace all

      result:

      "Tomorrow Kneel"
      "Tomorrow Kneel"
      "Tomorrow Kneel"
      
      1 Reply Last reply Reply Quote 0
      • S
        Savey Traveller
        last edited by Sep 21, 2017, 12:13 AM

        Hi

        Thanks for your reply, I’m not sure what I am doing wrong,

        My file content was Tomorrow Kneel
        I put into find
        Tomorrow (.*?)
        I put into Replace with
        Tomorrow Kneel
        Pointed to the folder where the file was stored
        selected Replace in Files

        But the content was not changed, I used a *.txt file type.

        Also, I should have mentioned that the second word is sometimes
        bracketed EG (Kneel)

        Hope this makes sense
        Regards
        savey

        1 Reply Last reply Reply Quote 0
        • S
          Savey Traveller
          last edited by Sep 21, 2017, 12:39 AM

          Ooooppps!

          I should have said

          I put into Replace with
          Tomorrow Running

          savey

          1 Reply Last reply Reply Quote 0
          • M
            Meta Chuh moderator
            last edited by Sep 21, 2017, 10:38 PM

            @Savey-Traveller
            yes, it is because your original example was within double quotes and the new example is not.

            regex search and replace always needs to know where to start and where to end
            so if your example now is Tomorrow Situps instead of “Tomorrow Situps” you have to tell regex to search from the desired word till the end of the line:
            Tomorrow (.*?)$

            (the $ tells regex to search until the end of the line)

            if you use the above search with the quotes it won’t find the string because your new example does not have any quotes.

            ps: maybe, just maybe it’s easier if you post the exact text for which you need the replace for, because fictional examples like yours often don’t reflect the task you need.

            1 Reply Last reply Reply Quote 0
            • S
              Savey Traveller
              last edited by Sep 22, 2017, 8:12 AM

              Hi

              a very good idea, thanks for your help and patience.

              The current data is like this, but there may bemore variables
              and actually it is the data in the second group that needs to remain
              and the first group changed, also enginedata and the spacing remains
              unchanged and the brackets are essential. The actual lettering can be longer
              or shorter but the spaces must be the same

              EngineData ( gg1_4859 GG1_4859 )

              EngineData ( hvycoach GG1_4859 )

              EngineData ( hvycoach23a GG1_4859 )

              the altered data needs to look like this

              EngineData ( new_wagon GG1_4859 )

              Hope this makes sense

              thanks

              savey

              M 1 Reply Last reply Sep 23, 2017, 2:56 AM Reply Quote 0
              • M
                Meta Chuh moderator @Savey Traveller
                last edited by Sep 23, 2017, 2:56 AM

                @Savey-Traveller
                yes, this example and description makes perfect sense now
                here are two regex replace examples you can play around with

                original data:

                EngineData ( gg1_4859 GG1_4859 )
                EngineData ( hvycoach GG1_4859 )
                EngineData ( hvycoach23a GG1_4859 )
                EngineData ( gg1_4872 GG1_4872 )
                

                search mode: regular expression

                replace example 1, only replace the first group if GG1_4859 is present in the second group:

                find what:
                \( (.*?) GG1_4859 \)
                
                replace with:
                \( new_wagon GG1_4859 \)
                

                result:

                EngineData ( new_wagon GG1_4859 )
                EngineData ( new_wagon GG1_4859 )
                EngineData ( new_wagon GG1_4859 )
                EngineData ( gg1_4872 GG1_4872 )
                

                replace example 2, this will search for any two data strings within the 2 brackets and only replace the first group, while keeping the second one in variable $2:

                find what:
                \( (.*?) (.*?) \)
                
                replace with:
                \( new_wagon $2 \)
                

                result:

                EngineData ( new_wagon GG1_4859 )
                EngineData ( new_wagon GG1_4859 )
                EngineData ( new_wagon GG1_4859 )
                EngineData ( new_wagon GG1_4872 )
                

                note that if you make a regex search for a ( or ) bracket, you have to search for ( or ) using a leading backslash, because the actual characters ( ) tell regex that anything within them are a specific regex expression

                1 Reply Last reply Reply Quote 1
                • S
                  Savey Traveller
                  last edited by Sep 23, 2017, 3:03 AM

                  Fantastic

                  thank you so much for your help.

                  Cheers

                  Daniel

                  1 Reply Last reply Reply Quote 1
                  • guy038G
                    guy038
                    last edited by guy038 Oct 8, 2017, 7:42 PM Oct 8, 2017, 7:41 PM

                    Hello @savey-traveller and @meta-chuh,

                    I’m quite late but here is a more simple syntax, for your regex S/R :

                    SEARCH \w+ +(?=GG1_4859) , with a space between the two + symbols

                    REPLACE new_wagon\x20

                    Notes :

                    • The first part \w+ + looks for a non-null range of consecutive word characters ( upper-case letter, lower-case letter, accentuated letter or digit ), followed by a **non-null range of consecutive space characters

                    • But, ONLY IF it’s followed by the exact string GG1_4859, (?=GG1_4859)

                    • If so, the word characters and space character(s), located before the string GG1_4859, are simply replaced by the exact string new_wagon, followed by a space character, \x20


                    So, given the original data :

                    EngineData ( gg1_4859 GG1_4859 )
                    EngineData ( gg1_1234 GG1_1234 )
                    EngineData ( hvycoach GG1_4859 )
                    EngineData ( hvycoach23a GG1_4859 )
                    EngineData ( gg1_4872 GG1_4872 )
                    

                    you should obtain the text, below, where, only, the lines 1, 3 and 4 have been modified -:))

                    EngineData ( new_wagon GG1_4859 )
                    EngineData ( gg1_1234 GG1_1234 )
                    EngineData ( new_wagon GG1_4859 )
                    EngineData ( new_wagon GG1_4859 )
                    EngineData ( gg1_4872 GG1_4872 )
                    

                    Best Regards,

                    guy038

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