• Login
Community
  • Login

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

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 820 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.
  • J
    JustinJudo
    last edited by Apr 16, 2023, 4:09 PM

    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?

    A P 2 Replies Last reply Apr 16, 2023, 4:24 PM Reply Quote 0
    • A
      Alan Kilborn @JustinJudo
      last edited by Apr 16, 2023, 4:24 PM

      @JustinJudo

      Try:

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

      J 1 Reply Last reply Apr 16, 2023, 4:30 PM Reply Quote 4
      • P
        PeterJones @JustinJudo
        last edited by Apr 16, 2023, 4:24 PM

        @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
        • J
          JustinJudo @Alan Kilborn
          last edited by Apr 16, 2023, 4:30 PM

          @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
          4 out of 4
          • First post
            4/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors