SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Append sequencial tracking code to each link using RegEx

    Hi,

    Regular expressions both baffle and scare me, so I thought I'd best throw this out to the experts.

    I create a large monthly emailer and the client insists on a unique tracking code on each link.

    This is the tracking code

    ?cmpid=FS005

    So I append a letter to this until I reach Z then I start the alphabet again but then append a 1

    ?cmpid=FS005a
    ?cmpid=FS005b
    ?cmpid=FS005c

    ....

    ?cmpid=FS005z
    ?cmpid=FS005a1
    ?cmpid=FS005b1
    ?cmpid=FS005c1

    etc

    And the margin for *ahem* human error is huge as there are a lot of links. Assuming the tracking codes are already in place is there a way to append these links sequentially with RegEx?

    Hope so!

    Mikee

  2. #2
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand correctly, the base code is already present in the text and you need to modify it, you're not dealing with the point at which the code is initially inserted, right? (If you're doing the initial insertion, it would be more appropriate to just append the unique bit to the code before inserting it rather than using a regex.) Try something like this:
    Code:
    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my $letter        = 'a';
    my $pass          = '';
    # The leading ? needs to be escaped because it has special meaning in a regex
    my $tracking_code = '\?cmpid=FS005';
    
    for my $line (<DATA>) {
      $line =~ s/($tracking_code)/$1$letter$pass/g;
      print $line;
      ($letter, $pass) = next_sequence_code($letter, $pass);
    }
    
    sub next_sequence_code {
      my ($letter, $pass) = @_;
      if ($letter eq 'z') {
        $letter = 'a';
        $pass++;
      } else {
        $letter++;
      }
      return ($letter, $pass);
    }
    
    __DATA__
    ?cmpid=FS005
    ?cmpid=FS005
    A link http://example.com/index?cmpid=FS005
    And the tracking code ?cmpid=FS005 with more text after it
    Two ?cmpid=FS005 tokens in one ?cmpid=FS005 coded line
    Output:
    Code:
    ?cmpid=FS005a
    ?cmpid=FS005b
    A link http://example.com/index?cmpid=FS005c
    And the tracking code ?cmpid=FS005d with more text after it
    Two ?cmpid=FS005e tokens in one ?cmpid=FS005e coded line

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Firstly apologies for the stupidly late reply!

    How would I run this script onto a HTML page?

    Cheers


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •