Simon
Simon

Reputation: 125

BASH: Regex -> empty result

I'm using bash but I do not get a bash rematch... Every online regex check tool worked fine for this string and regex.

#!/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}

Upvotes: 1

Views: 131

Answers (2)

micster
micster

Reputation: 758

You have to remove the duplicate _ at the end of your regex :

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

Upvotes: 1

Diego Torres Milano
Diego Torres Milano

Reputation: 69228

Parsing json with bash it's not a good idea, as others said, jq is the right tool for the job.

Having said that, I think

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

would work. Notice the '-' as the last char in the set, to avoid being interpreted as a range.

Upvotes: 1

Related Questions