regex in bash expression

前端 未结 3 711
渐次进展
渐次进展 2021-01-07 12:25

I have 2 questions about regex in bash expression.

1.non-greedy mode

local temp_input=\'\"a1b\", \"d\" , \"45\"\'
if [[ $temp_input =~ \\\".*?\\\         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-07 13:03

    This is my first post, and I am very amateur at bash, so apologies if I haven't understood the question, but I wrote a function for non-greedy regex using entirely bash:

    regex_non_greedy () {
        local string="$1"
        local regex="$2"
        local replace="$3"
    
        while [[ $string =~ $regex ]]; do
            local search=${BASH_REMATCH}
            string=${string/$search/$replace}
        done
    
        printf "%s" "$string"
    }
    

    Example invocation:

    regex_non_greedy "all cats are grey and green" "gre+." "white"
    

    Which returns:

    all cats are white and white
    

提交回复
热议问题