Re: Detect presence of numbers 1-9
by projekt21 (Friar) on Apr 08, 2002 at 08:57 UTC
|
if ( $scalar =~ /^[12345]+$/ ) {
print "match";
} else {
print "no match";
}
Update: as crazyinsomniac pointed out you can also write:
if ( $scalar !~ /[^12345]/ ) {
print "match";
}
alex pleiner <alex@zeitform.de>
zeitform Internet Dienste | [reply] [d/l] [select] |
|
if ( $scalar =~ /[^12345]/ ) {
print "Illegal parameter: $scalar";
} else {
print "Acceptable parameter: $scalar";
}
Maybe some would disagree but to me I like to say my regexes out loud to understand them and I find
if it matches any character that isn't a 1-5 then it is bad
much easier (read faster) to understand than
if it does not match any character that is not a 1-5 then it is good.
I agree im probably being a bit pedantic but I think you can see what I mean...
Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look. | [reply] [d/l] [select] |
|
If you do have a sequence like that, you can make it look even cleaner by writing it:
$scalar =~ /^[1-5]+$/
or
$scalar !~ /[^1-5]/
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue. | [reply] [d/l] [select] |
|
I'd avoid the double-negative tests because they would match the empty string (and undef, but you do have warnings enabled, right?). In most cases, it's usually better to state what you want, not what you don't want. :-)
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
| [reply] |
|
if ( $scalar =~ /^[12345]+$/ ) {
Please note that $ also matches before a trailing \n character, and that this regex also matches "111\n". But I'd go for the negative match anyway, because it seems more natural to me.
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk
| [reply] |
|
# or even:
$scalar =~ /[^1-5]/ ? &is_bad : &is_good;
print $scalar =~ /[^1-5]/ ? "bad" : "OK!";
$scalar !~ /[^1-5]/ && print "OK!";
print "OK!" unless $scalar =~ /[^1-5]/;
print "OK!" if $scalar !~ /[^1-5]/;
STATMWTDI cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
Re: Detect presence of numbers 1-9
by jmcnamara (Monsignor) on Apr 08, 2002 at 09:15 UTC
|
You could use a regex to either match the characters that you want or alternatively to not match the characters that you don't want:
#!/usr/bin/perl -wl
use strict;
my $scalar = '1335';
print "Found" if $scalar =~ /^[1-5]+$/;
print "Found" if $scalar !~ /[^1-5]/;
--
John.
| [reply] [d/l] |
Re: Detect presence of numbers 1-9
by perlplexer (Hermit) on Apr 08, 2002 at 13:01 UTC
|
unless ($str =~ tr/1-5//c){
print "Valid\n";
}else{
print "Invalid\n";
}
--perlplexer | [reply] [d/l] |
Re: Detect presence of numbers 1-9
by simon.proctor (Vicar) on Apr 08, 2002 at 09:46 UTC
|
If you felt brave enough you could combine the above with tie and then make all the tests transparent to your program. Look at the tie manpage for details. | [reply] |
|
If you felt brave enough you could combine the above with tie and then make all the tests transparent to your program.
A try (untested)
package Tie::Match;
use strict;
sub TIESCALAR {
my ($class, $regex) = @_;
return bless [undef, $regex], $class;
}
sub STORE {
my ($self, $new) = @_;
$self->[0] = $new =~ $self->[1] ? $new : undef;
}
sub FETCH {
my ($self) = @_;
return $_->[0];
}
sub UNTIE { }
sub DESTROY { }
__END__
=head1 NAME
Tie::Match - Tie a scalar that is undef when not matching a regex
=head1 SYNOPSIS
tie my $scalar, 'Tie::Match', qr/^[1-5]+$/;
while ($scalar = <>) {
if (defined $scalar) {
print "Match: $scalar\n";
} else {
print "No match.\n";
}
}
=head1 DESCRIPTION
Ties a scalar. The scalar can be set, but will be undef if the
new value does not match the given regex.
=head1 URL
http://perlmonks.org/?node_id=157404
=cut
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk
| [reply] [d/l] |
|
I think, this is a huge overkill :) Imagine, we'd have to compose a Tie::FooThing package every time we face a simple branching algorithm?
What this guy needs is probably some good intro into regexen and some good example variants of code, like tachyon provided.
| [reply] |
•Re: Detect presence of numbers 1-9
by merlyn (Sage) on Apr 08, 2002 at 16:20 UTC
|
| [reply] |