regular expression - why don't the last words change their order?
-
good day. I have some sentences with many words. For example:
word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15.
I made a regex, to change the order of the words.
SEARCH:
(\w+)\h+(\w+)\h+(\w+)\h+(\w+)\h+(\w+)\h+
Replace BY:\4 \1 \3 \5 \2
The problem, is that only the first 10 words change their order. the last 4-5 words do not change their position. Why ?
-
@Robin-Cruise said in regular expression - why don't the last words change their order?:
the last 4-5 words do not change their position. Why ?
Your regex has 1 or more horizontal spaces behind every word. In your example the 15th word does not have a space behind, rather a ‘.’. Therefore the regex will not do those 5 words.
Terry
-
@Terry-R said in regular expression - why don't the last words change their order?:
Your regex has 1 or more horizontal spaces behind every word. In your example the 15th word does not have a space behind, rather a ‘.’. Therefore the regex will not do those 5 words.
good day, @Terry-R I do not understand. Can you give me a response formulated in regex, a good solution? Because I don’t think it has to do with space or
'.
-
How about reading up on the special codes such as \h to know what they cover. For example try
http://rexegg.com/regex-quickstart.html
As well as documentation as provided in the FAQ section.I’m not on my PC but I’d suggest removing that last \h+ in your regex. Although you are using the regex in a replace function you can see what it will capture or not by using the find button and seeing each group it does capture one at a time. Just move the replace window so it doesn’t cover the data, that’s the way I test.
Terry
-
@Robin-Cruise said in regular expression - why don't the last words change their order?:
(\w+)\h+(\w+)\h+(\w+)\h+(\w+)\h+(\w+)\h+
yes, you right. Maybe I should use
+.*?
or just.*?
instead of\h+
-
@Robin-Cruise said in regular expression - why don't the last words change their order?:
Maybe I should use +.? or just .?
I would remove that last \h+ as your replacement text is not putting anything back behind that 5th word. By removing it the space or period characters would remain, thus only the words would change order, nothing else would be affected.
Terry
-
@Terry-R said in regular expression - why don't the last words change their order?:
I would remove that last \h+ as your replacement text is not putting anything back behind that 5th word. By removing it the space or period characters would remain, thus only the words would change order, nothing else would be affected.
I change a little simple:
SEARCH:
(\w+) (\w+) (\w+) (\w+) (\w+)
REPLACE BY:
\5 \1 \4 \2 \3
WORKS. The single problem, is that the last words doen not change in place of the first words… only after the 10 word…
-
maybe I did something wrong…
-
then your data is different to what you think it is.
Did you double check by making all symbols visible? -
@Robin-Cruise said in regular expression - why don't the last words change their order?:
maybe I did something wrong…
I’m thinking you did. My LAST suggestion was to JUST remove that last \h+ as your regex was working on all other groups excluding one’s with
.
instead of 1 or more horizontal spaces.Did you try that at all?!
Terry
-
@Terry-R said in regular expression - why don't the last words change their order?:
@Robin-Cruise said in regular expression - why don't the last words change their order?:
maybe I did something wrong…
I’m thinking you did. My LAST suggestion was to JUST remove that last \h+ as your regex was working on all other groups excluding one’s with
.
instead of 1 or more horizontal spaces.Did you try that at all?!
Terry
yes, but the same thing, this will change the 5 words after the first ten words. But if I want word15 to be in the place of word1, will not work…
-
@Ekopalypse said in regular expression - why don't the last words change their order?:
then your data is different to what you think it is.
Did you double check by making all symbols visible?hi. how can I do this?
-
@Robin-Cruise said in regular expression - why don't the last words change their order?:
But if I want word15 to be in the place of word1, will not work…
This appears to be a change in requirement. Your 1 example had 15 words and your regex was working with 5 words at a time, so your example would deal with the first 5 words, then the second set of 5 words. It failed on the third set of 5 words as it had the
\h+
.In that example with the
\h+
missing it should deal with ALL 3 sets of 5 words, one set at a time. Now however your reply that I copied above suggests you want the set of 15 words dealt with as 1 set.I’m going to give you the benefit of doubt as it also seems that English is not your primary language. So instead of giving yet more solutions to a possibly changing requirements I need you to tell us exactly what you require. I also need you to give more examples, showing both before and after processing views.
If you do not provide that additional information I’m opting out of this!
-
View->Show Symbol->Show All Characters
But as @Terry-R wrote, it seems that you are changing your requirements and this is something that does not work well with regexes.
If there is a language barrier here, try using an online translator and posting in your native language and the translated English version.
What I have written here was translated with DeepL.com