Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Conditional constant in regular expression

Reply
Thread Tools

Conditional constant in regular expression

 
 
Fritz Bayer
Guest
Posts: n/a
 
      04-09-2005
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-09-2005
Fritz Bayer wrote:
> 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.


<snip>

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


No.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      04-09-2005
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:umper;

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.


--
Matija
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-09-2005
Matija Papec wrote:
> 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:umper;
>
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-09-2005
Fritz Bayer <(E-Mail Removed)> wrote:

> I
> don't want to depend on any perl



Then you are in the wrong newsgroup.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      04-09-2005
X-Ftn-To: Gunnar Hjalmarsson

Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>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.



--
Matija
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      04-09-2005
Matija Papec <(E-Mail Removed)> wrote:
>>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.


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

^^^^^^
Should be: ([^?]+)

Axel

 
Reply With Quote
 
Fritz Bayer
Guest
Posts: n/a
 
      04-10-2005
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Fritz Bayer wrote:
> > 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.

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

>
> No.


Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
avoid constant conditional expression warning Sensorflo C Programming 2 01-07-2004 11:51 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments