CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Regex to insert associate ID into link

 



vallely
Novice

Oct 3, 2001, 7:18 PM

Post #1 of 3 (3782 views)
Regex to insert associate ID into link Can't Post

I give up!

I want to use a regex to replace qid= and all text until the > in a link with an associate ID string.

original link (The 10 digit number after ASIN varies but always remains 10 digits)
----------------------------
<a href=/exec/obidos/ASIN/0805063773/qid=1002143618/sr=1-22/ref=sr_1_7_22/102-5166113-5780135>Some Text</a>

----------------------------------------
I want
--------------------------------------------------
<a href=http://www.amazon.com/exec/obidos/ASIN/0805063773/associate-id>Some Text </a>

Thanks in advance for any help.




chengkai
stranger

Oct 4, 2001, 10:13 AM

Post #2 of 3 (3776 views)
Re: Regex to insert associate ID into link [In reply to] Can't Post

Suppose your html string is stored in a file called replace.txt then the follow one line code should solve your problem:

perl -ne "$pos = index ($_, 'qid'); pos ($_)=$pos; s/qid=(.*?)\//ASIN\/0805063773\/associate id/g; print" replace.txt

we first use index to find out the qid position and then reset the position pointer by using pos function. From there, we can use s//g to replace the string to the desired string. the /g modifier has to be added to make the position pointer change in effect. The other stuff should be pretty clear to you now!!



vallely
Novice

Oct 5, 2001, 2:57 AM

Post #3 of 3 (3770 views)
Re: Regex to insert associate ID into link [In reply to] Can't Post

Thanks chengkai.
That works great for me.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives