Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
sed-JSON正则表达式_Json_Regex_Sed - Fatal编程技术网

sed-JSON正则表达式

sed-JSON正则表达式,json,regex,sed,Json,Regex,Sed,很抱歉提出这样一个假问题,但这是我第一次使用curl命令,现在我从某处获得了这个命令来提取以下字符串 {“success”:true,“results”:1,“total”:1,“more”:false,“offset”:0,“hits”:[{“path”:“/home/users/Vq7DPVRHzGVK--OTJsHs”,“摘录”:“name”:“Vq7DPVRHzGVK--OTJsHs”,“title”:“Vq7DPVRHzGVK--OTJsHs”,“lastModified”:“2017

很抱歉提出这样一个假问题,但这是我第一次使用
curl
命令,现在我从某处获得了这个命令来提取以下字符串

{“success”:true,“results”:1,“total”:1,“more”:false,“offset”:0,“hits”:[{“path”:“/home/users/Vq7DPVRHzGVK--OTJsHs”,“摘录”:“name”:“Vq7DPVRHzGVK--OTJsHs”,“title”:“Vq7DPVRHzGVK--OTJsHs”,“lastModified”:“2017-03-03” 16:45:46,“创建”:“2017-03-03 16:45:46”}]}

我使用以下脚本将
curl
输出传输到
sed

sed -e 's/^.*"path":"\([^"]*\)".*$/\1/
结果:

/主页/用户/Vq7DPVRHzGVK——OTJsHs

有人能解释一下这里的正则表达式是怎么工作的吗?如何只得到Vq7DPVRHzGVK--OTJsHs的结果,而不包括/home/user路径?

正则表达式:
*“path\”:“\K[\/\w]+(?=\/)\/\K[^”]+

正则表达式:
*“path\”:“\K[\/\w]+(?=\/)\/\K[^”+
说明:

s/   ^.*"path":"\([^"]*\)".*$   /  \1   /
      ----------^------------     ---^---
             Pattern         Replacement string 
正则表达式如何工作:

^.*         # Match beginning of input string & anything else
"path":"    # Up to literal string `"path":"`
\([^"]*\)   # Then match slash and match + group anything up to a double quote `"`
".*$        # Match double quote and the rest of input string
通过替换字符串
\1
您将整个匹配部分替换为第一个捕获组,该组是路径值双引号之间的所有内容,除了开始斜杠

您需要的是将捕获组从捕获整个部分更改为最后一部分:

s/^.*"path":"[^"]*\/\([^"]*\)".*$/\1/
说明:

s/   ^.*"path":"\([^"]*\)".*$   /  \1   /
      ----------^------------     ---^---
             Pattern         Replacement string 
正则表达式如何工作:

^.*         # Match beginning of input string & anything else
"path":"    # Up to literal string `"path":"`
\([^"]*\)   # Then match slash and match + group anything up to a double quote `"`
".*$        # Match double quote and the rest of input string
通过替换字符串
\1
您将整个匹配部分替换为第一个捕获组,该组是路径值双引号之间的所有内容,除了开始斜杠

您需要的是将捕获组从捕获整个部分更改为最后一部分:

s/^.*"path":"[^"]*\/\([^"]*\)".*$/\1/

我在sed中得到了这个错误:-e expression#1,char 39:unterminated's'commandSED不支持PCRE。Welcome@DEN:)我在sed中得到了这个错误:-e expression#1,char 39:unterminated's'commandSED不支持PCRE。Welcome@DEN:)@SLePort我做得很好,除了对revo的赞赏和赞美,对吗?@DEN抱歉,评论太快了。我没有看到你是一个有经验的用户…@SLePort我做的很好,除了对revo的赞赏和赞美,对吗?@DEN抱歉,评论太快了。我没看到你是一个有经验的用户。。。