anirudh_sml has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have an problem, I am struck in it please help me out.
The Problem is I want the two regular exp one for username and other one for id in this url
http://beta.websitename.com/tool/view/mb/file?username=anirudh007&id=4 +3242342
I want the separate user-name and id in a variable.In url username and id can interchange.that is the url can be
http://beta.websitename.com/tool/view/mb/file?id=3323213&username=anir +udh007
Thanks

Replies are listed 'Best First'.
Re: Regular Expresion
by apl (Monsignor) on May 14, 2009 at 12:05 UTC
    What did you try?

    Rather than being daunted by the problem, try breaking it down into simple steps.

    • Field starts with ? or &
    • Continues with either
      • username=
      • alphameric string
      or
      • userid=
      • numeric string
    • Field terminates with & or EOL
    How would you implement something like this?
      I think his problem isn't the "simple steps", but going from "simple steps" to a "complicated total".

      Given that the username may, or may not, preceed the userid, how would you proceed? How would you proceed if there could be 10 fields, in any order?

        I wouldn't use one regular expression. I'd use two -- one for the User ID, one for the name. State information tells you if they're both from the same line.

        I realize brevity is the soul of wit, but I occasionally prefer verbosity if it results in ease of support. (Translation: I'm not as good a programmer as the rest of you...)

Re: Regular Expresion
by Your Mother (Archbishop) on May 14, 2009 at 15:47 UTC

    When you're dealing with something in a known/spec'd format, it's usually best to reach for a parser.

    use URI; use URI::QueryParam; while ( <DATA> ) { chomp; my $uri = URI->new($_); print $uri, "\n"; for my $param ( qw( id username ) ) { printf(" %s --> %s\n", $param, join(", ", $uri->query_param($param)) ); } } __DATA__ http://beta.websitename.com/tool/view/mb/file?id=3323213&username=anir +udh007 http://beta.websitename.com/tool/view/mb/file?username=anirudh007&id=4 +3242342

    See recent: url get variables regex

Re: Regular Expresion
by JavaFan (Canon) on May 14, 2009 at 10:03 UTC
    I wouldn't do it with a regular expression, but this works at least on your examples:
    use 5.010; while (<DATA>) { chomp; /(?=.*username=(?<username>[^&]*))(?=.*id=(?<id>[^&]*))/ or next; say "username = $+{username}; id = $+{id}"; } __DATA__ http://beta.websitename.com/tool/view/mb/file?username=anirudh007&id=4 +3242342 http://beta.websitename.com/tool/view/mb/file?id=3323213&username=anir +udh007 username = anirudh007; id = 43242342 username = anirudh007; id = 3323213
Re: Regular Expresion
by Anonymous Monk on May 14, 2009 at 09:50 UTC
Re: Regular Expresion
by citromatik (Curate) on May 14, 2009 at 11:07 UTC

    Another possibility using split's:

    use 5.010; use strict; use warnings; while (<DATA>){ chomp; my %ps = map {my @k = split /=/; $k[0] => $k[1]} split /&/,(split /\ +?/)[1]; say "username = $ps{username}; id=$ps{id}" } __DATA__ http://beta.websitename.com/tool/view/mb/file?username=anirudh007&id=4 +3242342 http://beta.websitename.com/tool/view/mb/file?id=3323213&username=anir +udh007
    username = anirudh007; id=43242342 username = anirudh007; id=3323213

    citromatik