Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Exact Regex for port Number

by theravadamonk (Scribe)
on Jun 19, 2018 at 07:15 UTC ( [id://1216913]=perlquestion: print w/replies, xml ) Need Help??

theravadamonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks, I am looking for an EXACT REGEX for a port number?

a few e.g

:42744

:1024

It DON'T want to MATCH my date Jun 19 08:48:50 where : is also present.

I went to https://regex101.com/ and wrote a bit, It WILL catch : of DATE data too

this catches BOTH

:[0-9]+
Can I have this?
:[0-9]{4,5}

Your efforts r welcome.

Replies are listed 'Best First'.
Re: Exact Regex for port Number
by Corion (Patriarch) on Jun 19, 2018 at 07:18 UTC

    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.

      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:  <%-{-{-{-<

Re: Exact Regex for port Number
by hippo (Bishop) on Jun 19, 2018 at 08:08 UTC
Re: Exact Regex for port Number
by QM (Parson) on Jun 19, 2018 at 07:41 UTC
    Does the port number occur in a context that can also be matched or not matched? For instance, a look-behind might avoid matching a time, or you might match all dates with priority, then anything left might be a port number.

    Please can you demonstrate sample strings? (Hopefully you have a full sense of the sample space.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Exact Regex for port Number
by thanos1983 (Parson) on Jun 19, 2018 at 09:48 UTC

    Hello theravadamonk,

    Fellow Monks have tried to tackle your question but from my point of view we are not still able to understand the problem. Can you provide us a sample of string that you want to apply the regex to check the port?

    For example is it like <IP>:<PORT> or is through a URL e.g. http://www.test.com:80/, or maybe something else.

    Help us to help you :)

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1216913]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-26 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found