| [reply] |
my $var = "though test tot 1 2 3 tesset";
$var =~ s/(t.*?t)/($1 ne "test") ? "" : $1/ge;
print $var; # prints: esoesset
But, now you mention a further constraint that the words to be deleted
may not contain any 't's inside, which is not inferrable from your earlier
posts at all. Providing a good specification is much more than providing a
sample case (but providing test cases *is* important).
Anyway, here's a go at your new specs:
my $var = <<TT;
target blah foo test thought 123 though tempest
testament though tightest treatment thermostat tantamount taboo
TT
$var =~ s/(?!\btest\b)(\bt[^t\W]*t\b)//g;
print $var;
__END__
## Result:
blah foo test 123 though
testament though tightest treatment thermostat tantamount taboo
So, all the 't.*t' words on the second line remain because they contain
a 't' character within. All the 't.*t' words on the first line get
deleted except for 'test'.
| [reply] [d/l] [select] |
any words that start with "t", end with "t", but do not contain any other "t"s within
OK so that's \bt[^t]+t\b -- word-boundary, then a t, then one or more other characters not a t, then a t, then a word boundary.
Apart from the abbreviation "tt" this should be fine.
So "tent", "tesseract", "tot", "tort" and "test" itself will match this pattern.
However, "testament" will fail it because of the "t" in the middle.
Then you need a special case for "test" itself, which you can do with the /e modifier and the ternary operator, as in pg's example above.
So something like this:
#!/usr/bin/perl -w
use strict;
my $words='test Buffy testament Anya tot Willow tesseract
Faith tent';
$words =~ s/\b(t[^t]+t)\b/$1 eq "test" ? $1 : ''/ge;
print $words;
# prints 'test Buffy testament Anya Willow Faith';
Where the regex means "Find words matching t, something-not-t, then t at the end. Replace them with nothing, unless they're the word test, in which case, replace them with themselves".
You could replace the ternary thing with this more longwinded version if you liked:
$words =~ s/\b(t[^t]+t)\b/
my $temp = $1;
if($temp eq 'test'){
$temp
}else{
''
}/xge;
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
| [reply] [d/l] [select] |
| [reply] [d/l] |
$_ = "this is the wristwatch";
s/(t.*?t)/($1 ne "test") ? "" : $1/ge;
print;
__END__
he wrisch
Now, that might be exactly what you had in mind, but it
doesn't suit the requirements.
Abigail | [reply] [d/l] [select] |
s/\s*\bt(?!est)[^t\W]*t\b//g;
Makeshifts last the longest.
| [reply] [d/l] |