In article <[EMAIL PROTECTED]>, Janek Schleicher
wrote:
> Pedro Antonio Reche wrote at Wed, 11 Jun 2003 13:38:18 -0500:
>
>> I would like to match a string if it has only cero or more of a defined
>> set of characters.
>> For example:
>> if "GACT" are the characters, then
>>
>> GACTNGACT ## This string should not be matched because it has the extra
>> character N
>> GACCCCCCC ## This could be matched;
>>
>> Any help to solve this problem will be greatly appreciated.
>
> Just another (from my point of view direct) view is
>
> $string =~ /^[GACT]*$/;
>
> The anchor stands for the beginning of the string,
> [GACT]* stands for zero or more of the defined characters
> $ stands for the end of the string.
This is my "only slightly fancier" version - still uses DATA though;
(critique/advice always welcome):
#!/usr/bin/perl
use warnings;
use strict;
# zero_or_more
my $valid_chars = get_valid_chars();
my $regex = qr{^[$valid_chars]*$};
while (<DATA>) {
# this really does an "exclude all other possibilities" match
# (zero or more of valid characters)
# but I skip empty lines
next if /^$/;
chomp;
print check_line($., $_);
}
## end main ##
## beg subs ##
sub get_valid_chars {
print "Enter valid characters (i.e. \"ABCD\"): ";
chomp (my $input = <STDIN>);
return $input;
}
sub check_line {
my ($ln_nbr, $line) = @_;
my $result = "Line #" . $ln_nbr . ": " . $line;
$result .= " Pass!" if ( $line =~ $regex );
$result .= "\n";
return $result;
}
__DATA__
GACTNGACT
GACCCCCCC
GGAACCNNG
##
-K (only 379 messages behind)
--
Kevin Pfeiffer
International University Bremen
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]