Conditional constant in regular expression

Discussion in 'Perl Misc' started by Fritz Bayer, Apr 9, 2005.

  1. Fritz Bayer

    Fritz Bayer Guest

    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
    Fritz Bayer, Apr 9, 2005
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Apr 9, 2005
    #2
    1. Advertising

  3. Fritz Bayer

    Matija Papec Guest

    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.


    --
    Matija
    Matija Papec, Apr 9, 2005
    #3
  4. 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::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. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 9, 2005
    #4
  5. Fritz Bayer <> wrote:

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



    Then you are in the wrong newsgroup.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 9, 2005
    #5
  6. Fritz Bayer

    Matija Papec Guest

    X-Ftn-To: Gunnar Hjalmarsson

    Gunnar Hjalmarsson <> 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
    Matija Papec, Apr 9, 2005
    #6
  7. Fritz Bayer

    Guest

    Matija Papec <> 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
    , Apr 9, 2005
    #7
  8. Fritz Bayer

    Fritz Bayer Guest

    Gunnar Hjalmarsson <> wrote in message news:<>...
    > 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.
    Fritz Bayer, Apr 10, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Kiermaier

    constant string in regular expression

    Michael Kiermaier, Jul 12, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,983
    Eric J. Roode
    Jul 13, 2003
  2. VSK
    Replies:
    2
    Views:
    2,267
  3. Sensorflo

    avoid constant conditional expression warning

    Sensorflo, Jan 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    513
    Derk Gwen
    Jan 7, 2004
  4. Replies:
    13
    Views:
    12,893
    Kai-Uwe Bux
    Jan 22, 2007
  5. Alf McLaughlin
    Replies:
    9
    Views:
    142
    Alf McLaughlin
    Feb 10, 2006
Loading...

Share This Page