Php Regex在冒号后添加空格
我试图在冒号和字符或数字之间添加一个空格,如果没有空格:Php Regex在冒号后添加空格,php,regex,Php,Regex,我试图在冒号和字符或数字之间添加一个空格,如果没有空格: this is my sentence:hi it's midnight this is my sentence:2012 was a good year. this sentence should be ignored: ignore me 我希望它是这样的: this is my sentence: hi it's midnight this is my sentence: 2012 was a good year. this se
this is my sentence:hi it's midnight
this is my sentence:2012 was a good year.
this sentence should be ignored: ignore me
我希望它是这样的:
this is my sentence: hi it's midnight
this is my sentence: 2012 was a good year.
this sentence should be ignored: ignore me
我试过了,但没有成功。
我可以使用此选项获得“:X”或“:0”(它与冒号后面的空格不匹配,这很好):
但如果我替换为:
: $0
结果是:
this is my sentence: :hi it's midnight
this is my sentence: :2012 was a good year.
我用它来测试模式
我将使用PHP preg_replace函数
非常感谢您的帮助。
提前感谢。您只需将
:\s*
替换为:
:
:\s*
匹配:
,后跟任意数量的空格(或无空格)。以下是在VIM中发出的两个替换命令:
%s/:/:<space>/g
%s/:<space><space>/:<space>/g
%s/:/:/g
%s/:/:/g
当然,这是一种过分的做法,即发出的命令数量是两个,而不是一个
以下是二合一命令:
:%s/:<space>*/:<space>/g
:%s/:*/:/g
用于使其更清晰,因为按空格键插入的空格不可打印
另一种解决搜索空间不可打印性的方法:
:%s/:\s*/:<space>/g
:%s/:\s*/:/g
$0捕获整个匹配项(其中包含:
!)。看到$1和一个捕获组,或者,也许更好,一个前瞻性的。在vim中,我相信它是:s/:\(\s\)/:\1/
备选方案:stru-replace(stru-replace($x,,,,,,,,,,,,,,,,,,,)
。用冒号替换所有的“结肠”,然后用结肠替换冒号。谢谢,顺便问一下*做什么找到它:@DiegoVieira:\s
匹配一个空格字符<代码>\s*匹配任意数量的空白字符。
:%s/:<space>*/:<space>/g
:%s/:\s*/:<space>/g