Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Regex for matching URLs with username/password and token

by Anonymous Monk
on Mar 10, 2025 at 09:42 UTC ( [id://11164205]=perlquestion: print w/replies, xml ) Need Help??

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

I am looking for a Perl regex pattern that can match URLs in the format where either a token is provided or both a username and password are provided after the server URL. For example:
1. --server api.blr-ocp1.lab.rbbn.com --username mgore --password abc1 +23 2. --server api.blr-ocp1.lab.rbbn.com --token kfjshdssahdvkbvjkbj
The regex should be able to capture these variations and extract relevant parts like the server URL, username, and password or token. Any guidance or examples would be greatly appreciated.

Replies are listed 'Best First'.
Re: Regex for matching URLs with username/password and token
by choroba (Cardinal) on Mar 10, 2025 at 10:01 UTC
    What about named capture groups?
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; for my $string ( '--server api.blr-ocp1.lab.rbbn.com --username mgore --password ab +c123', '--server api.blr-ocp1.lab.rbbn.com --token kfjshdssahdvkbvjkbj' ) { if ($string =~ /^--server\s+(?<server>\S+)\s+ (?:--username\s+(?<username>\S+)\s+--password\s+(?<passwor +d>\S+) |--token\s+(?<token>\S+))/x ) { say 'Server: ', ${^CAPTURE}{server}; if (exists ${^CAPTURE}{token}) { say 'Token: ', ${^CAPTURE}{token}; } else { say 'Username: ', ${^CAPTURE}{username}; say 'Password: ', ${^CAPTURE}{password}; } } else { say 'No match'; } }

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      use Getopt::Long; for ( '--server api.blr-ocp1.lab.rbbn.com --username mgore --password ab +c123', '--server api.blr-ocp1.lab.rbbn.com --token kfjshdssahdvkbvjkbj', ) { local @ARGV = split; my( $server, $username, $password, $token ); GetOptions( 'server=s' => \$server, 'username=s' => \$username, 'password=s' => \$password, 'token=s' => \$token, ) or die; $server or die "--server is required\n"; $token && ($username || $password) and die "cannot provide --usern +ame or --password with --token\n"; ($username xor $password) and die "--username and --password must +be provided together\n"; $token || $username or die "must provide either --token or --usern +ame plus --password.\n"; if ($token) { } else # username+password { } }
Re: Regex for matching URLs with username/password and token
by Corion (Patriarch) on Mar 10, 2025 at 11:27 UTC

    It is mildly unclear to me whether you have a (curl or wget) command, or an https:// URL.

    For Curl commands, see HTTP::Request::FromCurl for example, which parses a Curl (or Wget) command line.

    For https:// URLs, see URI or Mojo::URL.

    If you have concrete examples, please post them here together with your code in <code>...</code> tags, then we can give you more concrete advice.

Re: Regex for matching URLs with username/password and token
by hippo (Archbishop) on Mar 10, 2025 at 10:59 UTC

    IIUC, this sounds like a job for URI. Is there some reason to go reinventing the wheel here?


    🦛

      No, it really does not.
Re: Regex for matching URLs with username/password and token
by Marshall (Canon) on Mar 10, 2025 at 13:14 UTC
    I would parse the input line into a hash table. Add code to test the keys to see what kind of situation that you are in.
    if (defined ($parms{username}) and defined($parms{password}) ) { print "username and password case\n";}
    etc....
    use strict; use warnings; use Data::Dump qw(pp); while (defined (my $line =<DATA>)) { chomp $line; next unless $line =~ /\S/; #skip blank lines my %parms = $line =~ /--(\w+)\s+([\w.-]+)/g; pp \%parms; } =output { password => "abc123", server => "api.blr-ocp1.lab.rbbn.com", username => "mgore", } { server => "api.blr-ocp1.lab.rbbn.com", token => "kfjshdssahdvkbvjkbj", } =cut __DATA__ 1. --server api.blr-ocp1.lab.rbbn.com --username mgore --password abc1 +23 2. --server api.blr-ocp1.lab.rbbn.com --token kfjshdssahdvkbvjkbj
Re: Regex for matching URLs with username/password and token
by Anonymous Monk on Mar 10, 2025 at 17:33 UTC

    Your title and text say you want to parse URLs, but your examples look like command line options. If your examples represent what you are really trying to parse, it looks very much like a job for split() (or at the very worst Text::ParseWords) and Getopt::Long. Assuming the following is in file parse_url

    #!/usr/bin/env perl use 5.010; use strict; use warnings; use Data::Dumper; use Getopt::Long qw{ GetOptionsFromArray }; my @arg = split /\s+/, $ARGV[0]; my %option; GetOptionsFromArray( \@arg, \%option, qw{ password=s server=s token=s username=s }, ) or die; print Dumper \%option; # ex: set textwidth=72 :

    running perl parse_url '--server api.blr-ocp1.lab.rbbn.com --username mgore --password abc1' produces

    $VAR1 = { 'server' => 'api.blr-ocp1.lab.rbbn.com', 'password' => 'abc1', 'username' => 'mgore' };

    That is, the hash contains key/value pairs representing whatever options actually appear in the line. The or die; branch will be taken if the line contains unknown options. Non-option arguments will remain in @arg.

    pre tags replaced by Code tags by Grandfather

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2025-05-21 01:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.