GJ.
GJ.

Reputation: 870

need to improve bash regexp

in my bash script I use a regexp to match a string of variable assignment, for instance:

  1. Var = Value
  2. var = Value;
  3. Var=Value
  4. Var=Value;
  5. Var Value

the regexp i developed: \s*${varName}\s*\={0,1}\s*.*\s*;{0,1}

this regexp can match every instances above but also another instance that I don't want, which is VarValue

I cannot think of a way to make my regexp to not match the VarValue instance.

Upvotes: 1

Views: 235

Answers (2)

elias
elias

Reputation: 15510

Modifying yours:

\s*${varName}(\s?[\s\=]\s?).+\s*;{0,1}

Upvotes: 2

David B
David B

Reputation: 2698

\s*{varName}(?:\s*=\s*|\s+)(\w+)

I didn't modify your regex since it seemed quite complicated for the job, but this one will match all cases listed above while not matching VarValue. Your data will be in group 1.

Play with the regex here.

Upvotes: 2

Related Questions