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

ER makes me er..... 1

Status
Not open for further replies.

webamoeba

Programmer
Sep 6, 2006
23
GB
Hi,

I've always had difficulties understanding REs, and today is no exception! I want to be able to replace any occurances of:

<person>XXX</person>

with:

'<a href="somePage.pl?id=XXX">'$this->get('XXX')->toString'</a>'

where XXX can be any length and is numeric. How do I do this???

Thanks.
 
perlretut:
Code:
s~<person>(\w+)</person>~<a href="somePage.pl?id=$1">$this->get($1)->toString</a>~g;

I'm not sure if the inclusion of $this->get($1)->toString will work in that example or not. Sometimes Perl code runs in a regexp like that, sometimes not. If not, just use a while loop, which gives you a little more freedom (and looks cleaner than the thing on perlretut with actually executing Perl within the regexp)

Code:
while ($variable =~ m~<person>(\w+)</person>~) {
   my $id = $1;
   my $string = $this->get($1)->toString;

   $variable =~ s~<person>$id</person>~<a href="somePage.pl?id=$1">$string</a>~;
}
 
#!/usr/bin/perl

undef $/;
$_ = <DATA>;
$/ = "\n";

s|<person>(\d+)</person>|<a href="somePage.pl?id=$1">'$this->get('$1')->toString'</a>|g;

print;

[blue]__DATA__
blah blah <person>43</person> blah blah <person>123</person> blah blah <person>998877</person> blah blah <person>3</person> blah blah <person>987123456</person> blah blah[/blue][/code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top