Reg Ex Alter and Replace

Posted on 2005-04-14
Last Modified: 2010-03-05
Hello All.

I need to change:

<previous art_id="[something]">[something2]</previous>
<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.
Question by:Igiwwa
    LVL 84

    Accepted Solution

    $content=~s /<previous art_id="[^"]*">(.*)<\/previous>/<a href="@{[encode($1)]}">$1<\/a>/gi;
    sub encode{
      local $_ = shift;
      s/ /+/g;
      return $_;
    print $content;

    Author Comment

    awesome, thank you.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now