Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading