Regular expression to match any URL value (Perl syntax)

I am trying to find a template that will allow me to select part of the value of the Parameter = Value element of the URL string. I would like it to be general enough so that I can replace the "Parameter" with any term and get its meaning.

For example, if the URL string (it always follows this general form):

' http://www.mysite.com/home.aspx?userid=53&transaction=2&viewport=property '

I need to be able to get the value of a part of a user ID or transaction or viewport or whatever is selective.

It is advisable that it be as simple as adding a parameter name, and it matches the eveything that follows its = in &. My attempts to create a general purpose matching string ... suck.

I can't use the javascript function or anything else, it should look like a perl regex match (This is for use with Apache JMeter, if you're interested).

I suck in regex: - / Thanks in advance for any help.

+3
source share
3 answers

In case it should be a regular expression,

/userid=([^&]*)/
+7
source

Using a regular expression to parse URIs may not work for you in all cases: URI components often have characters in them escaped, they can appear several times, etc.

Perl URI URI:

use strict;
use warnings;
use URI;
use Data::Dumper;

my $u = URI->new('http://www.mysite.com/home.aspx?userid=53&transaction=2&viewport=property');

my %q = $u->query_form;
print Dumper(\%q), "\n";

query_form , , :

print $q{'transaction'}, "\n";
+6

, : <variable>=(\w+)
: userid=(\w+)

, "&". .

JMeter, :
http://jmeter.apache.org/usermanual/regular_expressions.html

- relx perl:
http://www.regexplanet.com/advanced/perl/index.html

If you control the URL itself, you will probably be able to simply create 3 regular expression extractors (one for each variable). I'm a little curly, but easier than trying all this at once. Here is an article that has a few more specific examples:
http://community.blazemeter.com/knowledgebase/articles/65150-using-regex-regular-expression-extractor-with-jm

+4
source

All Articles