Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配除url模式内的空格以外的空格_Regex_Spaces - Fatal编程技术网

Regex 正则表达式匹配除url模式内的空格以外的空格

Regex 正则表达式匹配除url模式内的空格以外的空格,regex,spaces,Regex,Spaces,您好,我是regex新手,我正在尝试使用它捕获垃圾中的空格\s{2,},但不是,包括“url”中的空格:https://x.com/a/C25/XPS -连接-2013年5月。docx“。目前,我有一个场景,url还没有编码,所以里面可能包含空格 示例文本: "startofjunk junkjunkjunkjunk","url":"https://x.com/a/C25/XPS - Connection - May 2013.docx","contentsource":"AX",

您好,我是regex新手,我正在尝试使用它捕获垃圾中的空格
\s{2,}
,但不是,包括
“url”中的空格:https://x.com/a/C25/XPS  -连接-2013年5月。docx“
。目前,我有一个场景,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"
请帮忙。谢谢

描述 这个正则表达式将找到一个将所有多个空格替换为单个空格的方法,并将绕过url部分。在X个空格的序列中,第一个空格被放入组1,该组作为
\1
馈送到输出,并且忽略额外的空格。URL部分被绕过,因为如果它是作为
或语句的一部分遇到的,那么它将被捕获到组2中,然后由
\2
替换将其注入到输出中

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

源字符串 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"

使用前视来断言您的空格出现在“url”之前。同时使用后视,这样您的整个匹配就是多余的空格:

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

(?Hi@denomaters谢谢!我们应该添加什么来将匹配的空格修改为单个空格?例如:“StartOfJunkJunkJunkJunkJunkJunkJunkJunkJunk”
(?<=\s)\s+(?=.*"url":)