Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match all chars with equal # of dots 3

Status
Not open for further replies.

mpalmer12345

Programmer
Feb 16, 2004
59
US
This seems like an easy problem, but I am stumped!

How do I match all chars in a portion of the string and replace with an equal number of "." characters.
So "alt=testx" becomes alt=….."?
 
$alt = "testx";

$alt =~ tr/[\W]/\./c;

print $alt;

Sean.
 
I think you've misunderstood the tr/// operator there Sean.

tr///, despite how it looks, has nothing to do with regexps, so it doesn't support character classes. This means that it doesn't understand \W (since it's not a valid escape sequence. Turn on warnings if you don't believe me) and treats it as a W. Similarly, [ and ] are just treated as literal characters.

Also, there's no need to escape the '.' in the replacement, as it's interpreted as a literal '.'.

Therefore, the code you posted will replace any character that is not (that's the /c) a '[', a 'W' or a ']', which happens to do what's required in the example used.

I hope that clears some thing up.

As for the question at hand, here's a suggested solution, which will work for any string that is formatted like "name=value", turing the 'value' into an equal number of dots:
Code:
my $alt = 'alt=testx';

$alt =~ s/(?<==)(.+)$/'.' x length($1)/ge;

print "$alt\n";
 
ishnid -->

Thanks for the info, I am very new to regular expressions, but, I ran my code and it worked on my box.?

Any ideas why?


Sean.
 
Woops, sorry. I get it now. ;-)

Thanks for the update!

Sean.
 
Hm, looking it over, I must say that this is beautifully condensed code, but being a beginner, I can't figure out what the (?<==) part is doing. Also, I tested the code on this second example, where it doesn't seem to work.

$text = '<PRE><IMG SRC="/icons/blank.gif" ALT="thisisatest">';

This should output

<PRE><IMG SRC="/icons/blank.gif" ALT="***********">
 
Sorry mpalmer - didn't see your post until now.

(?<==) is a lookbehind assertion (see It looks for a '=' character before the part that we're replacing, without including it in the match (i.e. the '=' itself is not replaced).

As for the code not working, it's not supposed to do what you're looking for it to do. Here's a possible solution for that (i.e. all the values are enclosed in double quotes and we only want the last one to be replaced):
Code:
my $alt = '<PRE><IMG SRC="/icons/blank.gif" ALT="thisisatest">';
$alt =~ s/(?<==")([^=\s"]+)(?="[^="]*$)/'*' x length($1)/e;
print "$alt\n";
 
Just for kicks,

What's the code to change it back to its original from the dots

[;)
Paul
 
Thanks Dunc. Paul - it's not very efficient but this code should 'decode' the asterisks :p

Code:
#!/usr/bin/perl -w
use strict;
my $alt  = '<PRE><IMG SRC="/icons/blank.gif" ALT="******************">';
$alt =~  s/\*/(split('', 'saliumP tr'))[$_]/e for qw/6 1 4 2 7 3 0 7 1 7 0 5 1 9 8 1 0 0/;

print "$alt [;)\n";
exit;
 
Had a feeling that was coming

Try this one then

.... ... .... ...'. .... .. ...... ......

hnnh, hnnhnh
;P
 
yep - that's gotta be a star for ishnid!

$alt  = '<PRE><IMG SRC="/icons/blank.gif" ALT="********************">';
print "$alt\n";
$alt =~  s/\*/(split('', 'switch louda'))[$_]/e for qw/1 2 0 5 6 2 6 4 8 9 7 10 -6 -2 -4 -6 3 5 -1 3/;
print "$alt";


Kind Regards
Duncan
 
Code:
$alt =~  s/\*/(split('', 'eiNc nosh'))[$_]/e for qw/2 1 3 0 4 6 5 0 4 1 7 8 4 4 4 4 4 4 4 /;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top