Select every 2nd whitespace using regex

After lot of messing around with regex, I’ve come to a problem that I am currently unable to solve.
Would it be possible to select every 2nd whitespace within a string?

For instance I’ve got the following data:

132966.04 482047.765 132971.443 482044.858 132981.138 482063.821 132975.874 482066.312 132966.04 482047.765

What I would like to do is to select every 2nd whitespace occurrence and replace it with a comma. This would result in the following:

132966.04 482047.765,132971.443 482044.858,132981.138 482063.821,132975.874 482066.312,132966.04 482047.765

With regex I’ve been able to select every whitespace occurrence (?<! )\s(?! ), but not every other occurrence.

>Solution :

Replace (\s.*?)\s with $1,

Here is a demo: https://regex101.com/r/UQngQE/1

You can use \S* instead of .*?. The first expression matches zero or more NON-whitespaces (notice the uppercase S), while the second one matches zero or more characters in lazy mode.

Leave a Reply