Jenifer_justin
Jenifer_justin

Reputation: 167

cutting a portion of url using regular expression in perl

I have the following url :

http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration

Need to proper way to extract the value after "?" Need to find how to explode in perl just the http://example.com part out of the string, and store it in its own variable, split it and save in a variable before passing it.

Upvotes: 1

Views: 411

Answers (5)

chooban
chooban

Reputation: 9256

Is this just a simple split that's required? If so...

my $foo = "http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration";
my @values = split( '\?', $foo );
print $values[1];

There are better ways that are more URL aware, but if that does the trick...

Upvotes: 2

MkV
MkV

Reputation: 3096

An expansion of this answer, includes query_param:

use URI;
use URI::QueryParam;
my $url = 'http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration';
my $uri = URI->new( $url );
my @keys = $uri->query_param(); 
# @keys contains the query parameter names
my $team_name = $uri->query_param( 'team_name' ); 
# $team_name contains the value of the team_name parameter

Upvotes: 3

user1244533
user1244533

Reputation: 101

To extract the value after ?, use

$url="http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration";
($query) = $url =~ /.+?\?(.+)/;

To get the domain name from url and save in the save variable

($url) = $url =~ m{(http://.+?)/};

hope this will help

Upvotes: 1

user764357
user764357

Reputation:

This regex:

^http://([^/]*)/[^?]*\?(.*)$

when applied to this string:

http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration

will yield these captured patterns

1. stagingbugzilla.cpiv.com
2. team_name=General%20administration

The complete code for Perl would be:

$url = "http://stagingbugzilla.cpiv.com/html/estVerificationPool/estPendingBugs.php?team_name=General%20administration";
($domain, $query) = ($url =~ m{^http://([^/]*)/[^?]*\?(.*)$});

With $domain and $query being the parts you want, although using a built in library like Pilcrow suggested is probably wiser.

Upvotes: 1

pilcrow
pilcrow

Reputation: 58534

Don't do it yourself, use the URI module, which is designed to make sense of this kind of data.

my $uri = URI->new('http://hostname.com/...?...');
$uri->query;  # The value after the '?'
$uri->scheme; # "http"
$uri->host;   # hostname.com

Upvotes: 10

Related Questions