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


Oct 3, 2001, 7:18 PM

Views: 13044
Regex to insert associate ID into link

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.


Oct 4, 2001, 10:13 AM

Views: 13038
Re: Regex to insert associate ID into link

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!!


Oct 5, 2001, 2:57 AM

Views: 13032
Re: Regex to insert associate ID into link

Thanks chengkai.
That works great for me.