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?
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:
#!/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:
?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