正则表达式替换所有空格组之后行

问题描述:

好了...这个问题听起来很奇怪,但我的意思是这样的:正则表达式替换所有空格组之后行

我有一组特定的搜索,我想这组后每一个空间,但只在目前的路线上被剥离出来。我的具体的例子是这样的:

@subpackage Some Word Stuff 

@subpackage不接受空白,但我不知道的时候,我有很多很多这些线的修复。我想用正则表达式(我的IDE支持这个)去查找和替换,以去掉每个实例@subpackage之后的单词之间的空格。

编辑:清晰的例子也许

"@subpackage Some Word Stuff" -> "@subpackage SomeWordStuff" 
+0

难以理解:( – vks 2014-10-09 15:32:24

+1

所以,你要'部分文字Stuff'成为'SomeWordStuff'? – Grice 2014-10-09 15:33:57

+0

是'@ subpackage'始终在该行的开始? – 2014-10-09 15:40:29

利用下文正则表达式,用空字符串替换''
(只需使用replace_all)

# '~(?mi-)(?:(?!\A)\G|^@subpackage)[^ \r\n]*\K[ ]+~' 

(?xmi-)      # Inline 'Expanded, multiline, case insensitive' modifiers 
(?: 
     (?! \A)     # Matched before, start from here 
     \G       
    |       # or, 
    ^@subpackage    # '@Subpackage' at bol (remove '^' if not at bol) 
) 
[^ \r\n]*     # Not space or line breaks 
\K       # Don't include anything from here back in match 
[ ]+      # 1 or more spaces 

这是一个对所有非断行空格。

# '~(?mi-)(?:(?!\A)\G|^@subpackage)\S*\K[^\S\r\n]+~' 

(?xmi-)      # Inline 'Expanded, multiline, case insensitive' modifiers 
(?: 
     (?! \A)     # Matched before, start from here 
     \G       
    |       # or, 
    ^@subpackage    # '@Subpackage' at bol (remove '^' if not at bol) 
) 
\S*       # 0 or more, Not whitespace 
\K       # Don't include anything from here back in match 
[^\S\r\n]+     # 1 or more non-linebreak whitespaces 
+0

我不确定这是否正常工作,我在这里测试了一下:http:// www。 phpliveregex.com/。我相信我使用的IDE(PHP风暴)遵循preg_replace – 2014-10-09 19:17:24

+0

的方法@JoelWorsham - http://www.phpliveregex.com/p/7jj按'preg_replace'选项卡。 – sln 2014-10-09 19:36:45