Regex 使用sed查找引号内部并跳过转义引号
我有一个curl调用,它查询JIRA REST API并返回一个JSON字符串,如下所示(单行除外): 我试图使用以下sed脚本解析出“summary”字段:Regex 使用sed查找引号内部并跳过转义引号,regex,bash,sed,Regex,Bash,Sed,我有一个curl调用,它查询JIRA REST API并返回一个JSON字符串,如下所示(单行除外): 我试图使用以下sed脚本解析出“summary”字段: sed 's/^.*summary":"\([^"]*\)".*$/\1/' 如果“摘要”中没有转义\“的话,这很好——但当然,使用转义引号,我从示例中得到的结果是: the \ 我想要的输出是: the \"special\" field is not returning what is expected 或者更离奇的是: the
sed 's/^.*summary":"\([^"]*\)".*$/\1/'
如果“摘要”中没有转义\“的话,这很好——但当然,使用转义引号,我从示例中得到的结果是:
the \
我想要的输出是:
the \"special\" field is not returning what is expected
或者更离奇的是:
the "special" field is not returning what is expected
似乎我无法在sed中进行查找,是否有一种简单的方法可以在bash脚本中解决此问题?对于双引号的内部,您确实需要至少一种功能:
\K
(这样您就可以删除开场白)(?<=(?<!\\)")(?:\\"|[^"])*(?=")
echo '{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"36112","self":"https://jira.company.com/rest/api/2/issue/36112","key":"FOO-1218","fields":{"summary":"the \"special\" field is not returning what is expected"}}' | sed -e 's/\\"/_____/g' -e 's/^.*summary":"\([^"]*\)".*$/\1/' -e 's/_____/"/g'
the "special" field is not returning what is expected
请注意,
[^”]
通常可以跨多行运行,您通常会使用[^”\r\n]
进行控制,但是grep
无论如何只能逐行查看。对于双引号的内部,您确实需要至少一个功能:
\K
(这样您就可以删除开场白)(?<=(?<!\\)")(?:\\"|[^"])*(?=")
echo '{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"36112","self":"https://jira.company.com/rest/api/2/issue/36112","key":"FOO-1218","fields":{"summary":"the \"special\" field is not returning what is expected"}}' | sed -e 's/\\"/_____/g' -e 's/^.*summary":"\([^"]*\)".*$/\1/' -e 's/_____/"/g'
the "special" field is not returning what is expected
请注意,
[^”]
通常可以跨多行运行,您通常会使用[^”\r\n]
进行控制,但是grep
无论如何只能逐行查看。对于双引号的内部,您确实需要至少一个功能:
\K
(这样您就可以删除开场白)(?<=(?<!\\)")(?:\\"|[^"])*(?=")
echo '{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"36112","self":"https://jira.company.com/rest/api/2/issue/36112","key":"FOO-1218","fields":{"summary":"the \"special\" field is not returning what is expected"}}' | sed -e 's/\\"/_____/g' -e 's/^.*summary":"\([^"]*\)".*$/\1/' -e 's/_____/"/g'
the "special" field is not returning what is expected
请注意,
[^”]
通常可以跨多行运行,您通常会使用[^”\r\n]
进行控制,但是grep
无论如何只能逐行查看。对于双引号的内部,您确实需要至少一个功能:
\K
(这样您就可以删除开场白)(?<=(?<!\\)")(?:\\"|[^"])*(?=")
echo '{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"36112","self":"https://jira.company.com/rest/api/2/issue/36112","key":"FOO-1218","fields":{"summary":"the \"special\" field is not returning what is expected"}}' | sed -e 's/\\"/_____/g' -e 's/^.*summary":"\([^"]*\)".*$/\1/' -e 's/_____/"/g'
the "special" field is not returning what is expected
请注意,
[^”]
通常可以跨多行运行,您通常会使用[^”\r\n]
控制这些行,但是grep
只能逐行查看。对于这种有限的情况,您可以使用
vnix$ sed -n 's/.*summary":"\(\([^\\"]*\|\\.\)*\)".*/\1/p' file.json
the \"special\" field is not returning what is expected
在带引号的字符串中,不允许使用双引号,除非在文字反斜杠之后立即允许使用任何字符。字符类也不允许使用反斜杠,以防止反斜杠“泄漏”到错误的部分匹配中。字符类之后的重复只是为了避免不必要的回溯而进行的优化
任何试图概括这一点的尝试都会很快变得非常笨拙。有一个例子,它超过了一页,只是为了说明这一点是徒劳的。对于这种有限的情况,您可以使用以下内容
vnix$ sed -n 's/.*summary":"\(\([^\\"]*\|\\.\)*\)".*/\1/p' file.json
the \"special\" field is not returning what is expected
在带引号的字符串中,不允许使用双引号,除非在文字反斜杠之后立即允许使用任何字符。字符类也不允许使用反斜杠,以防止反斜杠“泄漏”到错误的部分匹配中。字符类之后的重复只是为了避免不必要的回溯而进行的优化
任何试图概括这一点的尝试都会很快变得非常笨拙。有一个例子,它超过了一页,只是为了说明这一点是徒劳的。对于这种有限的情况,您可以使用以下内容
vnix$ sed -n 's/.*summary":"\(\([^\\"]*\|\\.\)*\)".*/\1/p' file.json
the \"special\" field is not returning what is expected
在带引号的字符串中,不允许使用双引号,除非在文字反斜杠之后立即允许使用任何字符。字符类也不允许使用反斜杠,以防止反斜杠“泄漏”到错误的部分匹配中。字符类之后的重复只是为了避免不必要的回溯而进行的优化
任何试图概括这一点的尝试都会很快变得非常笨拙。有一个例子,它超过了一页,只是为了说明这一点是徒劳的。对于这种有限的情况,您可以使用以下内容
vnix$ sed -n 's/.*summary":"\(\([^\\"]*\|\\.\)*\)".*/\1/p' file.json
the \"special\" field is not returning what is expected
在带引号的字符串中,不允许使用双引号,除非在文字反斜杠之后立即允许使用任何字符。字符类也不允许使用反斜杠,以防止反斜杠“泄漏”到错误的部分匹配中。字符类之后的重复只是为了避免不必要的回溯而进行的优化
任何推广这一点的尝试都会很快变得非常笨拙。有一个例子,它超过了一页,只是为了说明这一点是徒劳的。您要求的是一个用
sed
编写的JSON解析器。对不起,这太疯狂了
下面是一个在python中执行此操作的合理方法的示例:
import requests
response = requests.get(JIRA_API_ENDPOINT, headers = JIRA_HEADERS)
obj = response.json()
obj['fields']['summary']
python中还有一个很好的JIRA API包装器,名为JIRA python。只要使用它,您就不必进行任何解析。我以前使用过它,效果很好。链接如下:
你的同事会感谢你的。你要的是用
sed
编写的JSON解析器。对不起,这太疯狂了
下面是一个在python中执行此操作的合理方法的示例:
import requests
response = requests.get(JIRA_API_ENDPOINT, headers = JIRA_HEADERS)
obj = response.json()
obj['fields']['summary']
python中还有一个很好的JIRA API包装器,名为JIRA python。只要使用它,您就不必进行任何解析。我以前使用过它,效果很好。Link h