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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern substitution question

Status
Not open for further replies.

user2base

Technical User
Oct 16, 2002
29
0
0
GB
Hello,
I've got an input text in which I want to replace every string like "match(value)" by "value" (and store all "value" in an array)

i.e:
bla bla bla
bla bla match(value1) bla bla
bla match(value2) bla bla
bla match bla bla
bla match(value3) bla

becomes:
bla bla bla
bla bla value1 bla bla
bla value2 bla bla
bla match bla bla
bla value3 bla

and I have @array=(value1,value2,value3);

the text is in a scalar $txt
what is the substitution to apply to $txt ??

Thanks.
 
The following assumes that you have read your data into @array1:

foreach $line(@array1) {
$line=~s/match\((.+)\)/$1/;
push(@values, $1);
print "$line\n";
}

Then see if you are getting what you want by:

foreach $line(@array1) {
print "$line\n";
}
foreach $entry(@values) {
print "$entry\n";
}

Hope it helps.
 
#!/usr/bin/perl

my $txt = q|
bla bla bla
bla bla match(value1) bla bla
bla match(value2) bla bla
bla match bla bla
bla match(value3) bla
|;

my @array = ($txt=~/match\((.+)\)/g);
$txt=~s/match\((.+)\)/$1/g;

print $txt . "\n\narray=".join(",",@array)."\n";


will print:

bla bla bla
bla bla value1 bla bla
bla value2 bla bla
bla match bla bla
bla value3 bla


array=value1,value2,value3


Unfortunately this means pattern matching twice, once to get the match values and the other to replace them. The /g modifier saves you having to write a loop to store and replace them all.

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top