Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex: find and replace

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(‘:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

% 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.

>Solution :

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

You probably want a leading \b.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading