Re: Can a regex match numbers?
From: Tore Aursand (tore_at_aursand.no)
Date: 11/18/03
- Next message: Steve Massey: "matching query."
- Previous message: Viren Konde: "Re: Strange characters"
- In reply to: Jabez wilson: "Can a regex match numbers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Tue, 18 Nov 2003 19:54:31 +0100
On Tue, 18 Nov 2003 16:37:06 +0000, Jabez Wilson wrote:
> Is it possible to specify number matching in a regex, i.e. can you have
> something like:
>
> my $match =~ m/match_number_if_number_is_less_than_255/
>
> instead of my $match =~ m/(\d{3})/;
>
> if ($1<=255){my @array = @array +$1}?
Yes, but would you? IMO, using regular expressions to check the value of
something creates less readable code. I tend to prefer to first check if
I'm really dealing with a number, and then checking the value of it in a
good old fashion way;
if ( $nr =~ m,^[-+]?\d+$, && $nr < 255 ) {
# Match
}
I don't need to write that regular expression every time, of course,
'cause I've been nice enough to put it in a module so that I only need to
write 'is_int($nr)' each time.
(And the actual implementation of the is_int() function is heavier; It
check if a number really _is_ a number - the above don't, really...)
-- Tore Aursand <tore@aursand.no>
- Next message: Steve Massey: "matching query."
- Previous message: Viren Konde: "Re: Strange characters"
- In reply to: Jabez wilson: "Can a regex match numbers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|