Google Answers Logo
View Question
 
Q: Another (simple) Perl question ( Answered,   1 Comment )
Question  
Subject: Another (simple) Perl question
Category: Computers
Asked by: corn_jan-ga
List Price: $5.00
Posted: 27 Jun 2002 03:35 PDT
Expires: 27 Jul 2002 03:35 PDT
Question ID: 34032
Hi,

I've got another Perl question:

Suppose $data contains the following link:
<a href="http://krant.telegraaf.nl/krant/vandaag/teksten/bin.bestaande.weg.verkeer.html"
onmouseover="window.status='Netwerk van tolwegen op palen'; return
true;">tolwegen</a>

Suppose then I would want the text part ("tolwegen") to be replaced by
the window.status part ("Netwerk van tolwegen op palen") so that the
new link would look like this:

<a href="http://krant.telegraaf.nl/krant/vandaag/teksten/bin.bestaande.weg.verkeer.html"
onmouseover="window.status='Netwerk van tolwegen op palen'; return
true;">Netwerk van tolwegen op palen</a>

or it would look like this (it doens't matter which of the two you
come up with):

<a href="http://krant.telegraaf.nl/krant/vandaag/teksten/bin.bestaande.weg.verkeer.html">Netwerk
van tolwegen op palen</a>

Could you give me a Perl script or part of a script which does this? 

Thanks,
Corne
Answer  
Subject: Re: Another (simple) Perl question
Answered By: iaint-ga on 27 Jun 2002 03:56 PDT
 
Hi corn_jan

The following regular expression should achieve what you require. It
puts the modified string into $newdata so that you still have the
original string available should you require it. I have attempted to
comment the regex so that you have some understanding of how it works.
Please do not hesitate to ask for any clarification.

I've tried to format this so that it fits into the Google Answers
line-width without breaking, but without a "preview" facility I can't
be sure there won't be any line breaks. I'll add a clarification if
the code gets completely mangled!

Regards
iaint-ga

----- BEGIN CODE (watch for word-wraps)

($newdata = $data) =~
s/(.+)                    # Grab everything up until ...
  window.status='(.+)'    # this point. Now grab everything
                          # between the single-quotes.
  ([^>]+)                 # And now we want everything until
                          # the > character
  (.+)                    # This will get discarded
  # And below we put it all back together again:
 /$1window.status='$2'$3>$2<\/a>/xi;

# We now have our new HREF tag in $newdata

----- END CODE
Comments  
Subject: Re: Another (simple) Perl question
From: ake-ga on 27 Jun 2002 05:29 PDT
 
A good book on the subject is "Mastering Regular Expressions" by
Jeffrey E.F. Friedl, O'Reilly UK; ISBN: 1565922573.

Sincerely, 
ake-ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy