for regex - is it possible to move a word from the beginning of the line to the end of the line?
-
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?
-
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 lineSearch:
(^\w+).*?(\w*$)
Replace by:\2\1 -
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. -
find :
^(\w+)\s(.*)\s(\w+)(\.)?
replace with :$3\x20$2\x20$1$4 -
thank you all. Of course, in the end I find myself a solution:
Search:
(^\w+)(.*?)(\w*)\.$
Replace by:\3\2\1. -
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
\bassertions ensure that the group\1and group3contain true words, i.e. the first and last character of group2are non-word characters -
In group
2we 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 sentencePaul John., the group2is just thespacechar between the two firstnames !
Best Regards,
guy038
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login