• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Reg Ex Alter and Replace

Hello All.

I need to change:

<previous art_id="[something]">[something2]</previous>
to
<a href="[something2]">[something2]</a>

However, the first for the first [something2] I need to replace special characters like whitespaces before i rewrite it.  So this wouldn't work:

$content=~s /<previous art_id=\"[a-zA-Z0-9._-]+\">(.*)<\/previous>/<a href=\"$1\">$1<\/a>/gi;

Because the first $1 still has characters that I want to replace for links.

For an actual example:

<previous art_id="insurance">Homeowner's Insurance</previous>
needs to be
<a href="Homeowner&#39;s+Insurance">Homeowner's Insurance</a>

Thanks for the help in advance!  Hope I made everything clear enough.
0
Igiwwa
Asked:
Igiwwa
1 Solution
 
ozoCommented:
$content=~s /<previous art_id="[^"]*">(.*)<\/previous>/<a href="@{[encode($1)]}">$1<\/a>/gi;
sub encode{
  local $_ = shift;
  s/ /+/g;
  s/'/&#39;/g;
  return $_;
}
print $content;
0
 
IgiwwaAuthor Commented:
awesome, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now