regex in bash expression

前端 未结 3 710
渐次进展
渐次进展 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:04

    1. Your regular expression matches the string starting with the first quotation mark (before ab) and ending with the last quotation mark (after ef). This is greedy, even though your intention was to use a non-greedy match (*?). It seems that bash uses POSIX.2 regular expression (check your man 7 regex), which does not support a non-greedy Kleene star.

      If you want just "ab", I'd suggest a different regular expression:

      if [[ $temp_input =~ \"[^\"]*\" ]]
      

      which explicitly says that you don't want quotation marks inside your strings.

    2. I don't understand what you mean. If you want to find all matches (and there are two occurrences of b here), I think you cannot do it with a single ~= match.

提交回复
热议问题