Given a string as the following, containing only 0
and 1
separated by spaces with an additional space at the end of each line.
I want to select only the spaces between the digits but not the one at the end of the line to search and replace them with ,
.
When I try to create a RegEx I either select all spaces or the surrounding digits too.
GIVEN WANTED NOT WANTED TOTALLY NOT WANTED
0 1 0 1 0,1,0,1 0,1,0,1, ,,,,
0 0 1 0 0,0,1,0 0,0,1,0, ,,,,
1 0 1 0 1,0,1,0 1,0,1,0, ,,,,
1 1 0 0 1,1,0,0 1,1,0,0, ,,,,
1 1 1 0 1,1,1,0 1,1,1,0, ,,,,
1 1 0 0 1,1,0,0 1,1,0,0, ,,,,
So, how can I only select those spaces that have a digit on both sides, but not the digits. Is there some kind of syntax feature that matches but does not select?
Working answers:
As multiple answers were correct I want to collect them here. The accepted answer is the first one because it is the shortest regex and completely does what is needed.
(?!$)
– please note there is a space
at the beginning of this regex.
(?<=\d)\s(?=\d)
or (?<=[01])\s(?=[01])
if your digits are either 0 or 1
Best Answer
if you're sure that your text contains only
1
and0
you can use thisregex
(?!$)
there is aspace
character at the beginning of regexcheck this Demo
and if you're not sure you can use this one
(?<=\d) (?=\d)
but this is going to work only if look ahead and look behind is supported , I don't know which language you're using exactly , this isPERL
regexcheck the Demo