Bash variable substitution with a regex not working as expected

倾然丶 夕夏残阳落幕 提交于 2020-08-19 17:57:28

问题


Given a bash variable holding the following string:

INPUT="Cookie: cf_clearance=foo; __cfduid=bar;"

Why is the substitution ${INPUT/cf_clearance=[^;]*;/} producing the output: Cookie: instead of what I'd expect: Cookie: __cfduid=bar;

Testing the same regex in online regex validators confirms that cf_clearance=[^;]*; should match cf_clearance=foo; only, and not the rest of the string.

What am I doing wrong here?


回答1:


Use the actual regular-expression matching features instead of parameter expansion, which works with patterns.

[[ $INPUT =~ (.*)(cf_clearance=[^;]*;)(.*) ]]
ans=${BASH_REMATCH[1]}${BASH_REMATCH[3]}

You can also use an extended pattern, which is equivalent to a regular expression in power:

shopt -s extglob
$ echo "${INPUT/cf_clearance=*([^;]);/}"



回答2:


Use sed:

INPUT=$(sed 's/cf_clearance=[^;]*;//' <<< "$INPUT")



回答3:


Like you have been told in comments, bash parameter substitution only supports glob patterns, not regular expressions. So the problem is really with your expectation, not with your code per se.

If you know that the expression can be anchored to the beginning of the string, you can use the ${INPUT#prefix} parameter substitution to grab the shortest possible match, and add back the Cookie: in front:

echo "Cookie: ${INPUT#Cookie: cf_clearance=*;}"

If you don't have this guarantee, something very similar can be approximated with a pair of parameter substitutions. Find which part precedes cf_clearance, find which part follows after the semicolon after cf_clearance; glue them together.

head=${INPUT%cf_clearance=*}
tail=${INPUT#*cf_clearance=*;}
echo "$head$tail"

(If you are not scared of complex substitutions, the temporary variables aren't really necessary or useful.

echo "${INPUT%cf_clearance=*}${INPUT#*cf_clearance=*;}"

This is a little dense even for my sophisticated taste, though.)



来源:https://stackoverflow.com/questions/48542352/bash-variable-substitution-with-a-regex-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!