If I have a string like this:
"word1 'word2' word3"
is it possible to use a regex replace to change the string to this:
"word1 word3 'word2'"
I know what word1 and word3 will be, but do not know what word2 will be, but it will always be in single quotes.
You can replace "word1 ('\w+') word3"
with "word1 word3 \1"
. The replace syntax might be different in other regex engines; I'm using .NET's which is based on Perl's.
\w+
matches a series of word characters, aka a word. You can change this if it does not fit your definition of word;\1
in the replace string means to use group one from the match, \2
means group two, etc.I would say :
s/"(word1)\s+('.+?')\s+(word3)"/"$1 $3 $2"/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With