Using a single regex line (this goes in a config file for an application) I need to capture the localpart of an email address.
If it consists of only numbers, pass it along unmodified.
If it has any non-numeric character on it, truncate to 11 chars max.
I made this simple program to test but if the localpart is over 11 characters there is no match (the whole email address is printed).
#!/usr/bin/env perl
my @emails = ('the-chuck.t1.norrisson@chuck.com', '358451399991@chucksphon.com', 'ph33t@gmail.com', 'the-average.guyisbald@example.com', 'alongnameit@11chars.com', 'alongnameitis@13chars.com', 'a1234567890@11charnum.com');
for my $email (@emails){
# will put $1$2 on the substituion spot
$email =~ s/^(\d+)@.+|^([a-zA-Z0-9._%+-]{1,11})@.+/ /;
print '===> ' . $email . " \n\n ";
}
Where am I going wrong?
You need to include a regex atom such as
\S*?in case the number of characters exceeds 11…Using this produces correct output: