Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    3
    4
    38
    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.
    • JustinJudo
      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 Kilborn PeterJones 2 Replies Last reply Reply Quote 0
      • Alan Kilborn
        Alan Kilborn @JustinJudo last edited by

        @JustinJudo

        Try:

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

        JustinJudo 1 Reply Last reply Reply Quote 4
        • PeterJones
          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
          • JustinJudo
            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
            Copyright © 2014 NodeBB Forums | Contributors