Home > OS >  regex: find and replace
regex: find and replace

Time:01-28

I need to substitute the string x(y(str)) with z(str)

It should also shrink the white spaces. For example: x ( y (str) ) should also be replaced with z(str)

I can write a simple perl one liner to replace 'x(y(' with 'z(':

% echo "x(y(str))" | perl -pi -w -e 's/x\s*\(y\s*/z/g;'

but I don't know how to replace '))' with ')' in the same one-liner.

CodePudding user response:

perl -pe's/ x \s* \( \s* y \s* (.*?) \s* \) \s* \) /z($1)/xg'

You probably want a leading \b.

  •  Tags:  
  • Related