Community
    • Login

    RegEx help - find & replace specific word at the end of a line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 822 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.
    • JustinJudoJ
      JustinJudo
      last edited by

      I have a text file which contains all of my game titles. Titles that begin with the word ‘The’ are recorded as:

      First Tree, The.

      This was done purely for alphabetical sorting purposes. However there are some occasions when it would be useful to have it listed normally (The First Tree).

      I would like:
      Find: “First Tree, The”
      Replace with: “The First Tree”

      But only do so when the word ‘the’ happens to be the last word in the title. Is there a RegEx find & replace method for such occurrences?

      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @JustinJudo
        last edited by

        @JustinJudo

        Try:

        Find: (?-s)^(.+?), The$
        Replace: The ${1}
        Search mode: Regular expression

        JustinJudoJ 1 Reply Last reply Reply Quote 4
        • PeterJonesP
          PeterJones @JustinJudo
          last edited by

          @JustinJudo ,

          But only do so when the word ‘the’ happens to be the last word in the title.

          You didn’t tell us how to know if it’s the last word in the title. Is that indicated by a period, like you showed in your first example? Or an end-of-line? Or something else?

          Assuming it’s a period (and you want to keep the period):

          • FIND = (?-s)^(.+), (The)\.
          • REPLACE = $2 $1.
          • SEARCH MODE = Regular Expression

          ----

          Useful References

          • Please Read Before Posting
          • Template for Search/Replace Questions
          • Formatting Forum Posts
          • Notepad++ Online User Manual: Searching/Regex
          • FAQ: Where to find other regular expressions (regex) documentation
          1 Reply Last reply Reply Quote 0
          • JustinJudoJ
            JustinJudo @Alan Kilborn
            last edited by

            @Alan-Kilborn said in RegEx help - find & replace specific word at the end of a line:

            Find: (?-s)^(.+?), The$
            Replace: The ${1}

            That’s exactly it! Thank you Alan.

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