Conditional constant in regular expression

F

Fritz Bayer

Hello,

I have a URL and I would like to extract the domain, path and query
part by applying only A SINGLE regular expression.

What makes the problem challenging is that I want to preprend a
constant to each capturing group, if and only if, the value is non
null.

So for example the following strings should yield the following
output:

http://www.example.com/path/index.html
domain: www.example.com path: path

http://www.example.com/path/index.html?somekey=value
domain: www.example.com path: path query: somekey=value

http://www.example.com/
domain: www.example.com

The words "domain:", "path:" and "query:" are the constants, which
should be prepended if a capturing group in non empty.

Is there a single regular expression, which can solve this problem? I
don't want to depend on any perl, java or on any other code.

Fritz
 
G

Gunnar Hjalmarsson

Fritz said:
I have a URL and I would like to extract the domain, path and query
part by applying only A SINGLE regular expression.

What makes the problem challenging is that I want to preprend a
constant to each capturing group, if and only if, the value is non
null.

Is there a single regular expression, which can solve this problem?

No.
 
M

Matija Papec

X-Ftn-To: Fritz Bayer
The words "domain:", "path:" and "query:" are the constants, which
should be prepended if a capturing group in non empty.

Is there a single regular expression, which can solve this problem? I
don't want to depend on any perl, java or on any other code.

use strict;
use Data::Dumper;

my @fields = qw/domain path query/;
my $dreg = qr{
^http://
([^/]+)/? #domain
(?: (.+)/)? #path
(?: [^?]+)? #document
(?: \? (.*))? #query
}xi;

my @url = qw{
http://www.example.com/path/index.html
http://www.example.com/path/index.html?somekey=value
http://www.example.com/
};
for my $u (@url) {
my %adr;
my $i = 0;
$adr{$fields[$i++]} = $_ for grep defined, $u =~ /$dreg/;
print Dumper \%adr;
}

Anyway, strongly consider cpan URL module as it covers complete RFC.
 
G

Gunnar Hjalmarsson

Matija said:
X-Ftn-To: Fritz Bayer
Is there a single regular expression, which can solve this problem? I
don't want to depend on any perl, java or on any other code.

use strict;
use Data::Dumper;

my @fields = qw/domain path query/;
my $dreg = qr{
^http://
([^/]+)/? #domain
(?: (.+)/)? #path
(?: [^?]+)? #document
(?: \? (.*))? #query
}xi;

my @url = qw{
http://www.example.com/path/index.html
http://www.example.com/path/index.html?somekey=value
http://www.example.com/
};
for my $u (@url) {
my %adr;
my $i = 0;
$adr{$fields[$i++]} = $_ for grep defined, $u =~ /$dreg/;
print Dumper \%adr;
}

That looks like Perl code to me. ;-)
 
M

Matija Papec

X-Ftn-To: Gunnar Hjalmarsson

Gunnar Hjalmarsson said:
That looks like Perl code to me. ;-)

Err, right; at first I thought that OP doesn't want to use external modules
but now I see that his request is even more extreme. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top