Bod has asked for the wisdom of the Perl Monks concerning the following question:
Good evening fellow Monks
I've just been writing a bit of code to partially hide an email address such that the owner of the address would recognise it but anyone else would find it difficult, at least to work out. I tried a simple substitution but hit a problem. How can I determine how many characters there are and replace them with that same number of full stops or asterisks or whatever?
I have come up with a three line solution but it seems like the sort of problem that should be able to be done with a regexp substitution
This produces this result:use strict; use warnings; my @email = ( 'someone@example.co.uk', 'andrew.test@some.company.co.uk', 'jo@abc.com', ); foreach my $em(@email) { print "$em - "; my ($name, $comp) = split /@/, $em; $em =~ /^(\w[\w|\.]).*@(\w\w).*\.(\w+)$/; $em = $1 . '.' x (length($name) - 2) . "\@$2" . '.' x (length($com +p) - length($3) - 2) . $3; print "$em\n"; }
This is one of those problems that seems relatively trivial until you actually come to do it! It also suffers as a difficult one to search for as Google wants to tell me how to match a range of lengths {x,y} or count the number times one string occurs in another.C:\Users\joolz\Perl>perl regtest.pl someone@example.co.uk - so.....@ex.........uk andrew.test@some.company.co.uk - an.........@so..............uk jo@abc.com - jo@ab..com
Is there a nice, simple, succinct way to do this?
I guess the nicest solution would change the number of characters properly displayed as a function of the overall length!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting characters within regexp
by tybalt89 (Monsignor) on May 08, 2021 at 23:12 UTC | |
|
Re: Counting characters within regexp
by LanX (Saint) on May 08, 2021 at 23:06 UTC | |
by Bod (Parson) on May 09, 2021 at 09:37 UTC | |
|
Re: Counting characters within regexp
by AnomalousMonk (Archbishop) on May 08, 2021 at 23:43 UTC | |
by Bod (Parson) on May 09, 2021 at 09:26 UTC | |
by hippo (Archbishop) on May 09, 2021 at 11:01 UTC | |
by LanX (Saint) on May 09, 2021 at 09:54 UTC | |
|
Re: Counting characters within regexp
by jwkrahn (Abbot) on May 09, 2021 at 02:59 UTC | |
|
Re: Counting characters within regexp
by kcott (Archbishop) on May 10, 2021 at 06:57 UTC |