in reply to [SOLVED] Regex : Limit value of a string

Hello PtitePomme, and welcome to the Monastery!

You’ve already received excellent answers; I just want to raise an issue that arises from your use of the special . character in the first capture: (gi.*). The expression .* matches any sequence of non-newline characters. So, even with an anchor to the end of the string, your regex will still match something like this:

my $input = 'GigabitEthernet1/0/49xxx7/0/48';

because (gi.*) will match GigabitEthernet1/0/49xxx.

Now, maybe this isn’t a problem; but as a general principle I’d be more comfortable with a regex that specifies the exact pattern to be matched. Making the * quantifier non-greedy won’t help here. But you can exclude / characters from the first match:

if ($input =~ m{ ^ (gi [^/]*) ([1-8]) / 0 / ([1-9] | [1-3][0-9] | 4[0- +8]) $ }ix) {

The first capture here is gi (any case) followed by a sequence of non-slash characters. Too fussy? Perhaps, but then — better safe than sorry!

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,