Community

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

    for regex - is it possible to move a word from the beginning of the line to the end of the line?

    Help wanted · · · – – – · · ·
    4
    6
    458
    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.
    • Robin Cruise
      Robin Cruise last edited by

      everyone. is it possible to move a word from the beginning of the line to the end of the line, and the one from the end to be place at the beginning of the line? It’s a change of places, viceversa

      For example:

      Paul is at home with his brother John.

      Must become:

      John is at home with his brother Paul.

      can this be done with regex?

      1 Reply Last reply Reply Quote 0
      • Robin Cruise
        Robin Cruise last edited by

        I try this regex, but not a great solution:

        (^\w+) - select the first word on the beginning of each line
        (\w*$) - select the last word at the end of the line

        Search: (^\w+).*?(\w*$)
        Replace by: \2\1

        Alan Kilborn 1 Reply Last reply Reply Quote 0
        • Alan Kilborn
          Alan Kilborn @Robin Cruise last edited by

          @Robin-Cruise

          This seems to work for your data, but there’s probably more to what you really need:

          find: (?-s)^(\w+)(.*?)(\w+)\.
          repl: \3\2\1.

          1 Reply Last reply Reply Quote 2
          • Abed99
            Abed99 last edited by

            find : ^(\w+)\s(.*)\s(\w+)(\.)?
            replace with : $3\x20$2\x20$1$4

            1 Reply Last reply Reply Quote 1
            • Robin Cruise
              Robin Cruise last edited by

              thank you all. Of course, in the end I find myself a solution:

              Search: (^\w+)(.*?)(\w*)\.$
              Replace by: \3\2\1.

              1 Reply Last reply Reply Quote 0
              • guy038
                guy038 last edited by

                Hello, @robin-cruise, and All,

                I suppose that, if the sentence contains a single word, followed with a full stop like Paul., it should not be concerned by the replacement, ins’t it ?

                So, the correct regex S/R is :

                SEARCH (?-s)^(\w+)\b(.+)\b(\w+)\.$

                REPLACE \3\2\1.

                Notes :

                • The \b assertions ensure that the group \1 and group 3 contain true words, i.e. the first and last character of group 2 are non-word characters

                • In group 2 we just can use a greedy quantifier + ( not +? ) as we’re looking for the last word of current line, anyway !

                • As the search regex contains three expressions with the + quantifier, this means that each group is not empty. And, in case of the minimal sentence Paul John., the group 2 is just the space char between the two firstnames !

                Best Regards,

                guy038

                1 Reply Last reply Reply Quote 2
                • First post
                  Last post
                Copyright © 2014 NodeBB Forums | Contributors