in reply to Exact Regex for port Number

The ssh service runs on port 22, so :22 is both, a valid number of seconds and a valid port number.

Your second approach would work, but it would miss port :443 for https.

Maybe you can be more specific for the situations where a port number is to be expected.

Replies are listed 'Best First'.
Re^2: Exact Regex for port Number
by theravadamonk (Scribe) on Jun 19, 2018 at 08:15 UTC

    Hi,Thank you ALL for your replies. in this case, I am a BIT lucky since I only have higher POTRS from 1024 to 65535 . So I can go with {4,5}

    So, I go with below code. Thanks once again. Hv a wonderful day.

    :[0-9]{4,5}

      But  :[0-9]{4,5} allows port numbers like 1000 and 99999; more range checking is in order. Maybe something like:

      c:\@Work\Perl\monks>perl -wMstrict -le "for my $pn (qw( :1 :23 :456 :999 :1000 :1023 :1024 :9999 :10000 :65535 :1024x :9999x :10000x :65535x :65536 :99999 :123456 :x :foo : ), '', ' ', @ARGV) { my $pn_ok = $pn =~ m{ : (\d{4,5}) (?! \d) }xms && $1 >= 1024 && $1 <= 65535; print qq{'$pn' }, $pn_ok ? 'in range' : 'BAD'; } " ':1' BAD ':23' BAD ':456' BAD ':999' BAD ':1000' BAD ':1023' BAD ':1024' in range ':9999' in range ':10000' in range ':65535' in range ':1024x' in range ':9999x' in range ':10000x' in range ':65535x' in range ':65536' BAD ':99999' BAD ':123456' BAD ':x' BAD ':foo' BAD ':' BAD '' BAD ' ' BAD


      Give a man a fish:  <%-{-{-{-<