【问题标题】:BASH: Regex -> empty resultBASH:正则表达式->空结果
【发布时间】:2018-03-25 22:31:54
【问题描述】:

我正在使用 bash,但我没有得到 bash 重新匹配...每个在线正则表达式检查工具都适用于这个字符串和正则表达式。

#!/bin/bash 
set -x
regex='hd_profile_pic_url_info": {"url": "([0-9a-zA-Z._:\/\-_]*)"'
str='{"user": {"pk": 12345, "username": "dummy", "full_name": "dummy", "is_private": true, "profile_pic_url": "censored", "profile_pic_id": "censored", "is_verified": false, "has_anonymous_profile_picture": false, "media_count": 0, "follower_count": 71114, "following_count": 11111, "biography": "", "external_url": "", "usertags_count": 0, "hd_profile_pic_versions": [{"width": 320, "height": 320, "url": "censored"}, {"width": 640, "height": 640, "url": "censored"}], "hd_profile_pic_url_info": {"url": "https://scontent-frt3-2.cdninstagram.com/vp/censored/censored_a.jpg", "width": 930, "height": 930}, "has_highlight_reels": false, "auto_expand_chaining": false}, "status": "ok"}'
[[ $str =~ $regex ]] && echo ${BASH_REMATCH}

【问题讨论】:

  • 使用jq 来获取你想要的值会简单得多。 echo "$str" | jq -r '.user.hd_profile_pic_url_info.url'
  • Just escape {. 并将- 移动到括号表达式的末尾。

标签: regex string bash


【解决方案1】:

用 bash 解析 json 这不是一个好主意,正如其他人所说,jq 是适合这项工作的工具。

说了这么多,我觉得

regex='hd_profile_pic_url_info": {"url": "[0-9a-zA-Z._:\/_-]*"'

会起作用的。注意 '-' 作为集合中的最后一个字符,以避免被解释为范围。

【讨论】:

  • 我从未使用过 jq,所以我没有想到。但是现在我的脚本可以工作了,ty :)
【解决方案2】:

您必须在您的正则表达式末尾删除重复的_

regex='"hd_profile_pic_url_info": {"url": "([0-9a-zA-Z._:\/\-]*)"'

【讨论】:

    猜你喜欢
    • 2014-12-23
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多