Reputation: 1150
I'm trying to add a space between the colon and the character or number if there is no space already:
this is my sentence:hi it's midnight
this is my sentence:2012 was a good year.
this sentence should be ignored: ignore me
And I want it to read:
this is my sentence: hi it's midnight
this is my sentence: 2012 was a good year.
this sentence should be ignored: ignore me
I've tried this with no success. Regex to insert space in vim
I can get the ":X" or ":0" using this (it doesn't match the space after the colon, which is good):
:[^\s]
But if I replace with:
: $0
It ends up like:
this is my sentence: :hi it's midnight
this is my sentence: :2012 was a good year.
I'm using http://www.gskinner.com/RegExr/ to test the pattern.
I'll be using PHP preg_replace function
Any help is much appreciated. Thanks in advance.
Upvotes: 2
Views: 3651
Reputation: 2419
Here are two substitution commands to be issued in VIM:
%s/:/:<space>/g
%s/:<space><space>/:<space>/g
Of course, it is an overkill, that is, the number of commands to be issued is two, not one.
Here is two-in-one command:
:%s/:<space>*/:<space>/g
<space>
is used to make it more clear as the whitespace inserted by pressing the spacebar is not printable.
Another approach to work around non-printability of space for searching:
:%s/:\s*/:<space>/g
Upvotes: 1
Reputation: 298206
You can just replace :\s*
with :<pretend this is a space>
:
preg_replace('/:\s*/', ': ', $text)
:\s*
matches :
followed by any number of spaces (or no spaces).
Upvotes: 6