use strict;
local $/;#####To read the whole content of a file
while(<DATA>){
while($_=~/\(\!-- #BeginEditable \"([^"]*)" --\)(.*?)\(\!-- #EndEd
+itable --\)/sgi){
print "IN:$1:$2\n";
##do your stuffs here
}
}
Punitha | [reply] [d/l] |
Punitha, you are a magician. I have made a donation to Perl Monks Foundation in your name, and I pledge also to study the regex you've written so that I can pass it on. Thank you for your timely help!
Charlie Pi
| [reply] |
WOW, i'm sure i've never tried .*? in a regex, so if that was supposed to make sense, ignore me.
Why would you put this into one line if it is already split into easily manipulated parts?
You might as well just use BeginEditable and EndEditable and set a switch so that only the lines in the middle are accepted:
#!/usr/bin/perl
use strict;
my $sw;
while (<DATA>) {
if ($_ =~ /BeginEditable/) {$sw="on";}
elsif ($_ =~ /EndEditable/) {$sw="off";}
elsif ($sw eq "on") {print "$_"}
}
__DATA__
psycho
alpha
(!_-%BeginEditable$$) nb. the "html" is irrelevent
disco
mater
tangle
(^-EndEditables)
glyph
It's unclear from your question what else you wanted to do. | [reply] |
.*? is perfectly legitimate in a Perl regex. Search for "non-greedy" sometime.
Your solution is great if the input actually has the tags in question conveniently on lines by themselves, but breaks otherwise. If you've ever worked with real users, you know that's unlikely unless the input was purely machine generated with that constraint in mind and never touched by the users.
| [reply] |
$_ = '<S>abc<E> <S>def<E>';
print("Greedy:\n"); print(" $1\n") while /<S>(.*)<E>/g;
print("Non-greedy:\n"); print(" $1\n") while /<S>(.*?)<E>/g;
Greedy:
abc<E> <S>def
Non-greedy:
abc
def
| [reply] [d/l] [select] |