Community

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

    regular expression - why don't the last words change their order?

    Help wanted · · · – – – · · ·
    3
    14
    144
    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 Robin Cruise

      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 ?

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

        @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

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

          @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 '.

          1 Reply Last reply Reply Quote 0
          • Terry R
            Terry R last edited by

            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

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

              @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+

              1 Reply Last reply Reply Quote 0
              • Terry R
                Terry R last edited by

                @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

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

                  @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…

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

                    maybe I did something wrong…

                    Ekopalypse Terry R 2 Replies Last reply Reply Quote 0
                    • Ekopalypse
                      Ekopalypse @Robin Cruise last edited by

                      @Robin-Cruise

                      then your data is different to what you think it is.
                      Did you double check by making all symbols visible?

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

                        @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

                        Robin Cruise 1 Reply Last reply Reply Quote 1
                        • Robin Cruise
                          Robin Cruise @Terry R last edited by

                          @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…

                          Terry R 1 Reply Last reply Reply Quote 0
                          • Robin Cruise
                            Robin Cruise @Ekopalypse last edited by

                            @Ekopalypse said in regular expression - why don't the last words change their order?:

                            @Robin-Cruise

                            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?

                            Ekopalypse 1 Reply Last reply Reply Quote 0
                            • Terry R
                              Terry R @Robin Cruise last edited by Terry R

                              @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!

                              1 Reply Last reply Reply Quote 4
                              • Ekopalypse
                                Ekopalypse @Robin Cruise last edited by

                                @Robin-Cruise

                                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

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