I have this string:
<img src="<?php echo CDN(); ?>images/logo.png" style="outline: 0; font-size: 100%; vertical-align: baseline; border-radius: 5px 5px 5px 5px; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0; border-left-width: 0; background-color: transparent;text-align: center;" />
Please don't point to inline styling. I know it's a mess. Just dealing with some legacy code.
I need to convert <?php echo CDN(); ?>images/logo.png" string to: <?php echo CDN('images/logo.png'); ?>images/logo.png". I am trying to make a regex for it using vim since there are multiple files. This is the regex that I have made:
:%s/"<?php echo CDN(); ?>\(.*\)"/"<?php echo CDN('\1'); ?>\1"/g
But the output that I get is this:
<img src="<?php echo CDN('images/logo.png" style="outline: 0; font-size: 100%; vertical-align: baseline; border-radius: 5px 5px 5px 5px; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0; border-left-width: 0; background-color: transparent;text-align: center;'); ?>images/logo.png" style="outline: 0; font-size: 100%; vertical-align: baseline; border-radius: 5px 5px 5px 5px; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0; border-left-width: 0; background-color: transparent;text-align: center;" />
The regex doesn't end at logo.png" but goes on to take everything after " in consideration as well. Can someone please tell me what is wrong with the regex?
EDIT
Thanks all for the answer. It worked. Now I am trying to write a sed regex so that I could apply this regex to every file. I got this:
cat test.php | sed -r 's/"<?php echo CDN\(\); ?>(.*)"/"<?php echo CDN\(\1\); ?>\1"/g'
As you might have guessed, it breaks. It works with <?php echo CDN\(\) but stops working the moment I add ?>. I don't know why.
Further edit
My mistake. here is the sed pattern: sed -r 's/"<\?php echo CDN\(\); \?>([^"]*)"/"<\?php echo CDN\("\1"\); \?>\1"/g'
You need non-greedy matching; i.e. use .\{-} (Vim's syntax for "0 or more, as few as possible", what's often written .*? in other regular expression dialects) instead of .*.
Since double quotes cannot be contained, you can further restrict this by disallowing the double quote [^"] instead of matching all kinds of characters (.); with this, you can still safely use the greedy multiplier [^"]*:
:%s/"<?php echo CDN(); ?>\([^"]*\)"/"<?php echo CDN('\1'); ?>\1"/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With