• Login
Community
  • Login

How to replace multiple words?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 3.6k 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.
  • B
    Bahaa0013
    last edited by Feb 28, 2023, 4:16 PM

    Is there a way to replace multiple words with multiple values at the same time?
    I mean like translating.
    example text:
    “Lorem ipsum dolor sit amet, consectetur adipiscing elit.”

    What I want to replace:
    (this: that)
    amet: friend
    Lorem: human
    elit: fruit

    Output:
    “human ipsum dolor sit friend, consectetur adipiscing fruit.”

    So is there any regex commands to do that?

    If this future is available in notepad++ it will help so much to edit huge txt file, Instead of replacing one by one…

    A P 2 Replies Last reply Feb 28, 2023, 4:20 PM Reply Quote 0
    • A
      Alan Kilborn @Bahaa0013
      last edited by Feb 28, 2023, 4:20 PM

      @Bahaa-Eddin-ツ

      You’re talking about a “replace from list” capability.
      It doesn’t exist unless scripting is used.
      Try searching this forum for “replace from list”.

      B 1 Reply Last reply Feb 28, 2023, 4:39 PM Reply Quote 3
      • P
        PeterJones @Bahaa0013
        last edited by Feb 28, 2023, 4:30 PM

        @Bahaa-Eddin-ツ ,

        Also, since I couldn’t find my post when I used @Alan-Kilborn’s search term, I will mention I wrote a “translation bot” script, where you put in a group of pairs in the script, then run the script, and it “translates” it. The example is English -> Some Other Language. But the same logic works for English1 -> English2 or Lorem Ipsum to English or whatever.

        1 Reply Last reply Reply Quote 2
        • B
          Bahaa0013 @Alan Kilborn
          last edited by Feb 28, 2023, 4:39 PM

          @Alan-Kilborn
          @PeterJones

          Thanks, I found what I want:

          Find: (Lorem)|(amet)|(elit)
          Replace: (?1human)(?2friend)(?3fruit)

          P 1 Reply Last reply Feb 28, 2023, 4:48 PM Reply Quote 2
          • P
            PeterJones @Bahaa0013
            last edited by Feb 28, 2023, 4:48 PM

            @Bahaa-Eddin-ツ ,

            That will definitely work for a short list of changes.

            If that’s sufficient for you, that’s great. Alan and I tend to answer assuming that the real requirements is to have a longer list than the example (which wouldn’t be practical in the alternation regex like you showed).

            B 1 Reply Last reply Feb 28, 2023, 5:05 PM Reply Quote 2
            • B
              Bahaa0013 @PeterJones
              last edited by Feb 28, 2023, 5:05 PM

              @PeterJones said in How to replace multiple words?:

              That will definitely work for a short list of changes.
              If that’s sufficient for you, that’s great. Alan and I tend to answer assuming that the real requirements is to have a longer list than the example (which wouldn’t be practical in the alternation regex like you showed).

              I think it works perfectly with long list of words too : ), But it needs some time to add +120 word, and then I have to add again 120 word to replace with…

              I’m new in notepad++, That’s why I chose the easy method :>

              P 1 Reply Last reply Feb 28, 2023, 5:28 PM Reply Quote 0
              • P
                PeterJones @Bahaa0013
                last edited by PeterJones Feb 28, 2023, 5:40 PM Feb 28, 2023, 5:28 PM

                @Bahaa-Eddin-ツ ,

                The Find what field is character-limited to 2047 characters, so 200 words at 8 characters per word plus the three characters for ( and ) and | means 2200 characters, which will not fit.

                But if you ever really do go to that many long words, then you have the option to switch to a scripting language as suggested above, or break it into a couple of ~2000-character expressions rather than one >2000-character expression.

                But personally, if the regex is more than ~100 words, I would question my own sanity in using it, because it would be easier for me to put the 100 pairs in a readable form like the TranslationBot script than to build up a regex that long and hope I didn’t make a mistake in creating the find and replace expressions – off-by-one could be killer in that situation!

                –
                edit: changed from 1000 (bad memory) to 2047 (actual threshold); add third paragraph

                A 1 Reply Last reply Feb 28, 2023, 6:08 PM Reply Quote 2
                • A
                  Alan Kilborn @PeterJones
                  last edited by Alan Kilborn Feb 28, 2023, 6:12 PM Feb 28, 2023, 6:08 PM

                  Find: (Lorem)|(amet)|(elit)
                  Replace: (?1human)(?2friend)(?3fruit)

                  Really, if you’re going to go “long” with that syntax, you could have a script do it – create the syntax for you. But…if you’re making the scripting leap, maybe it is best to go “all the way”.

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