【发布时间】: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
{. 并将-移动到括号表达式的末尾。