I’m trying to change strings like this:
<a href='../Example/case23.html'><img src='Blablabla.jpg'
To this:
<a href='../Example/case23.html'><img src='<?php imgname('case23'); ?>'
And I’ve got this monster of a regular expression:
find . -type f | xargs perl -pi -e \
's/<a href=\'(.\.\.\/Example\/)(case\d\d)(.\.html\'><img src=\')*\'/\1\2\3<\?php imgname\(\'\2\'); \?>\'/'
But it isn’t working. In fact, I think it’s a problem with Bash, which could probably be pointed out rather quickly.
r: line 4: syntax error near unexpected token `('
r: line 4: ` 's/<a href=\'(.\.\.\/Example\/)(case\d\d)(.\.html\'><img src=\')*\'/\1\2\3<\?php imgname\(\'\2\'); \?>\'/''
But if you want to help me with the regular expression that’d be cool, too!
Teaching you how to fish:
Use a separator other than
/for thesoperator because/already occurs in the expression.Cut down on backslash quoting, prefer
[.]over\.because we’ll shellquote later. Let’s keep backslashes only for the necessary or important parts, namely here the digits character class.Capture only the variable part. No need to reassemble the string later if the most part is static.
Use
$1instead of\1to denote backreferences.[^']*means everything until the next'.To serve now as the argument for the Perl
-eoption, this program needs to be shellquoted. Employ the following helper program, you can also use an alias or shell function instead:Run it and paste the program body, terminate input with Ctrl+d, you receive:
Put this together with shell pipeline.