2013-06-04 79 views
1

嗨内部空间我是新来的正则表达式,我试图用这个来追赶空间的垃圾\s{2,}包括"url":"https://x.com/a/C25/XPS - Connection - May 2013.docx"内的空间。目前,我有一个场景,url尚未编码,因此它可能包含空格。正则表达式匹配的空间,除了URL模式

示例文字:

"startofjunk  junkjunkjunkjunk","url":"https://x.com/a/C25/XPS - Connection - May 2013.docx","contentsource":"AX","returpath":null,"detailpath":"https://ax.sample.com/Rep>ositories/form.aspx?path=C25/96/99&mode=Read","detailspath2":"samplepath" 

所需的文本:

"startofjunk junkjunkjunkjunk","url":"https://x.com/a/C25/XPS - Connection - May 2013.docx","contentsource":"AX","returpath":null,"detailpath":"https://ax.sample.com/Rep>ositories/form.aspx?path=C25/96/99&mode=Read","detailspath2":"samplepath" 

请帮助。由于

回答

0

说明

此正则表达式会找到一个空间内的全部替换多个空格,并且将绕过URL部分。在X个空格序列中,第一个空格放置到组1中,作为\1输入到输出,并且忽略其他空格。 URL部分被绕过,因为如果它作为|或语句的一部分遇到,则将其捕获到组2中,然后通过\2替换将其注入到输出中。

正则表达式:(\s)\s*|("url":"[^"]*"),替换:\1\2

enter image description here

源字符串

"startofjunk  junkjunkjunkjunk","url":"https://x.com/a/C25/XPS - Connection - May 2013.docx","contentsource":"AX","returpath":null,"detailpath":"https://ax.sample.com/Rep>ositories/form.aspx?path=C25/96/99&mode=Read","detailspath2":"samplepath" 

这个PHP例子包括PHP为例,简单地表明,正则表达式的作品

<?php 
$sourcestring="your source string"; 
echo preg_replace('/(\s)\s*|("url":"[^"]*")/im','\1',$sourcestring); 
?> 

$sourcestring after replacement: 
"startofjunk junkjunkjunkjunk","url":"https://x.com/a/C25/XPS - Connection - May 2013.docx","contentsource":"AX","returpath":null,"detailpath":"https://ax.sample.com/Rep>ositories/form.aspx?path=C25/96/99&mode=Read","detailspath2":"samplepath" 
+0

嗨@Denomales谢谢!我们应该添加什么来修改匹配空间到单个空间?像这样:“startofjunk junkjunkjunkjunk” – user2450064

+0

我刚更新了这个以满足单个空间的需求。 –

0

使用前瞻来断言您的空间在“url”之前出现。还可以使用一看,后面让你整场比赛是多余的空格:

(?<=\s)\s+(?=.*"url":) 

要去除多余的空间,与空白(即无)更换整场比赛,或者如果您的应用程序语言允许的话,删除的整场比赛。