Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

complex substitution 1

Status
Not open for further replies.

Aleksei

Technical User
Mar 4, 2002
10
NL
I am trying to do a complex multi-line substitution while parsing a file. My data is in the form of
Code:
"KEYWORD identifier (multiline\ndata) (more data);"
. I've got the proper matching statement and the processing of the data figured out (put it in a subroutine), but now I want to apply it to all matches. Can I do something like
Code:
s/<expr>/procedure($+)/mg;
?


 
yes, putting 'g' at the end of your regexp will make it match globally.

post your actual code to see if there's something specific to it that would prevent this from working. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Well, my problem is calling the s/<expr>/procedure($+)/mg;
It replaces the matched string with the string 'procedure()'
 
If you want to evaluate the right side before doing the replacement, add an 'e' switch.

s/<expr>/procedure($+)/[red]e[/red]mg

An illustration,
Code:
#!perl

$str = 'a string that 
exists on several lines and 
contains the string &quot;string&quot; 
multiple times';
print &quot;$str\n\n\n&quot;;

$str =~ s/string/procedure($&)/ges;
print &quot;$str\n&quot;;
sub procedure
{
my $var = shift;
# caught string, return twine
return('twine');
}
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
what about something like this

sub transform($){
my $var = shift;
return(uc($var)); # just to do something with it he re
}

while(<>){
while(/<expr>/){
my $tmp = transform($&);
s/<expr>/$tmp/;
}
}
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
The /e modifier did the trick - I just found it in man perlop myself.

Thnx guys!

 
[medal] I never knew that - thanks! Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top