I'd like to replace all of the php short tags to full blown ones.
Example source text:
<a href="<?=$baseurl?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ?>></a>
Expected final result:
<a href="<?php echo $baseurl; ?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ?>></a>
I got as far as this:
Find what: <?=(.*)?>
Replace with: <?php echo \1; ?>
But the result is:
<a href="<?php echo $baseurl?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ; ?>></a>
So it doesn't really work when there are multiple php opening and closing tags on one line.
How do I fix this? Thanks!
Yes, because the * operator is greedy and will try to match everything up to the final ?. You can fix this by matching everything except the ? in the closing tag:
Find what: <?=([^?]*)?>
Replace with: <?php echo \1; ?>
Put this in the Search for field:
<\?=([^?]+)\?>
And this in the replacement field:
<?php echo \1; ?>
When you use *, Notepad++ matches as much as it can, so you need to tell it what you don't want to match so it stops without finding the whole line:
Find: <?=([^?]*)?>
Replace: <?php echo \1; ?>
Using * in your regex makes it "greedy" and you don't want that. You want the first occurrence of ?> to be matched and not the last. Also, the ? character is special in regexes so you need to escape it too:
<\?=([^?]+)\?>
Related
My regex:
(<property\sname=\"uri\"\s[value=\"htttp:\/\/]+(\d+\.)+\d+)+
Text sample:
<bean id="journeyWSClient" parent="abstractClient" class=" lib.JourneyWSClient">
<property name="uri" value="http://192.24.342.432:20010/some/path/1_0_0"/>
<!-- Fortuna -->
<!-- property name="uri" value="http://164.7.19.11:20010/some/path/1_0_0"/ -->
It works on http://regexr.com/, however when I put the regex in bash script, it doesn't work. Are there some characters I need to escape? Ideas?
Bonus cookie for extracting the IP with only one regex.
Replace the \d with [0-9] and \s with [[:space:]], and adjust the IP matching part as ([0-9]+(\.[0-9]+)+) (or simplify it to ([0-9.]+)) so as to be able to get its value with ${BASH_REMATCH[1]}:
text='<property name="uri" value="http://192.49.200.142:20010/some/path/1_0_0"/>'
regex='<property[[:space:]]name="uri"[[:space:]]value="http://([0-9]+(\.[0-9]+)+)'
if [[ $text =~ $regex ]]; then
echo ${BASH_REMATCH[1]};
fi
See the IDEONE demo
See if this works for you..
value="http:\/\/(([0-9].?)+):
This worked for me if you have IP only with http pattern
grep http TEXT_SAMPLE_FILE |sed -E 's;.*value="http://(([0-9].?)+):.*;\1;g'
I need to search and replace on file this:
<?php echo lang_xxxx; ?>
To this:
<?php echo $lang['lang_xxxx']; ?>
How to do this? The lang_xxxx is not unique, can exists anothers like lang_yyyy, lang_zzzz, etc...
You could use the following expression:
Live Example
(lang_[^;]*?);
and replace it with:
$lang['$1'];
or:
$lang['\1'];
Results:
Input: <?php echo lang_xxxx; ?>
Output: <?php echo $lang['lang_xxxx']; ?>
Input: <?php echo lang_es; ?>
Output: <?php echo $lang['lang_es']; ?>
I was looking to build a sed that would globally kill short tags in my scripts as there is a LOT of legacy stuff floating around that needs to be banished. I was working of a regex but it's being greedy so am looking for a non-greedy sed that will replace all occurances of
<\?=(.*)\?> ## Pattern
<?php echo $1; ?> ## Replacement
This WILL turn <?=$foo?> into <?php echo $foo; ?> but it will also turn <?=$foo?><?=$bar?> into <?php $foo?><?=$bar; ?> as well as duplicates the line of the match.
The SED looks like sed -i -e "s/<?=\(.*\)?>/<?php echo \1; ?>/g" file
My answer would assume that
<?=$foo?><?=$bar?> ## in
<?php echo $foo; ?><?php echo $bar; ?> ## out
You can try with
sed 's/<[?]=\([^?]*\)/<?php echo \1; /g' file
<?php echo $form->create(); ?>
<?php echo $form->hidden('id'); ?>
<?php echo $form->input('name')); ?>
<?php echo $form->submit('Save'); ?>
<?php echo $form->end(); ?>
I want to replace "; ?" with " ?".
I used the vi command ":%s/; \?/ \?/g" to do that. I got the following output
<?php echo $form->create() ??>
<?php echo $form->hidden('id') ??>
<?php echo $form->input('name')) ??>
<?php echo $form->submit('Save') ??>
<?php echo $form->end() ??>
Actually, I need the following output.
<?php echo $form->create() ?>
<?php echo $form->hidden('id') ?>
<?php echo $form->input('name')) ?>
<?php echo $form->submit('Save') ?>
<?php echo $form->end() ?>
Can you give the explanation for this strange behavior?
The \? is the vi-regular-expression for the normal ? in other program's regular expressions.
You can look at :h regex to verify this (or more precisely :h E61).
So when using ; \? you match ; (no space) and ;<space> (one space). From these matches the greediest one (see E61 - as many as possible) will be replaced with a ? thus resulting in two question-marks (one new one and the old one) when there is a space present.
The correct expression would be: s/; ?/ ?/g
EDIT: Fixed explanation to be more precise.
:%s/; \?/ \?/g
; \? match ;<space> zero or one time, and it replaces them with ?.
So you get the original ? with another ?.
So something like this
<h2 style='margin-top: 10px;'>
<?php echo $title; ?>
</h2>
should become
<h1 style='margin-top: 10px;'>
<?php echo $title; ?>
</h1>
I use this code perl -pi -e 's/<h2(.*)<\/h2>/<h1\1<\/h1>/g' * but it works only if tags are in one line.
There is no need to pair up the opening and closing tags. Simply change all opening <h2> tags to '` and then do the same for all closing tags. Like this
perl -pie 's|<h2\b|<h1|g; s|</h2\b|</h1|g;' *
perl -pi.bak -we 's#</?h\K2#1#g' yourfile
\K will preserve matched text before it. That way we don't have to put it back after matching it.
No *nix environment to test, but
% find . -name "*.html" -exec sed -e s/\<h2/\<h1/g -e 's|</h2|</h1|g' {} \;
might get you started. No Perl required.
You can use perl like this:
perl -0pe -i.bak 's#<h2(.*?)</h2>#<h1$1</h1>#gs' file
Below code works for me
use strict;
use warnings;
open FILE, 'inputfile.txt';
my $contents = do { local $/; <FILE> };
close FILE;
$contents =~ s/<h2(.*?)<\/h2>/<h1$1<\/h1>/sg;
print $contents;
Note that $/ is input record seperator.It is newline \n by default.
We create a localized version of $/ so its previous value gets restored after the block finishes.