Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Help with regexp to capture quotes

by duperdog (Initiate)
on Nov 28, 2001 at 02:18 UTC ( #127933=perlquestion: print w/replies, xml ) Need Help??
duperdog has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a reexp that will return the value after the equal sig without quotes. I know I can trim the quotes after, but I was trying to get this into a shorter statement.
Here is the sub I have written so far:
sub get_arg { shift =~ /\W([^=]+)/; $1; } my $reqid = get_arg(FCP_REQID=103806); # returns 103806 my $login = get_arg(FCP_LOGIN="USER/PASS"); #returns "USER/PASS", des +ire USER/PASS

I am currently doing the following, but it seems like I should be able to take care of the problem in the sub, so I don't have to add the tr/ to each call :
(my $login = get_arg(FCP_LOGIN="USER/PASS") =~ tr/\"//d; __DATA__ FCP_REQID=103806 FCP_LOGIN="USER/PASS" FCP_USERID=3944 FCP_PRINTER="noprint"


davorg: expanded node title

Replies are listed 'Best First'.
Re: Help with regexp
by boo_radley (Parson) on Nov 28, 2001 at 02:53 UTC
    requires rhs to be inside quotes.

    while (<DATA>) { print $_; /="?([^"]+)"?/; print $1,"\n"; } __DATA__ FCP_REQID=103806 FCP_LOGIN="USER/PASS" FCP_USERID=3944 FCP_PRINTER="noprint"

    Dragonchild : you're only capturing one character!

      I would prefer to use split.

      while (<DATA>) { print; ( my $after = (split /=/)[1] ) =~ s/"//g; print $after; } __END__ FCP_REQID=103806 FCP_LOGIN="USER/PASS" FCP_USERID=3944 FCP_PRINTER="noprint"

      Charles K. Clarkson

      Half a cookie is better than
      Thanks for everyone's help, this one is what I was looking for.

Re: Help with regexp
by dragonchild (Archbishop) on Nov 28, 2001 at 02:41 UTC
    sub get_arg { shift =~ /="?([^="])/; $1; }
    or, in two steps
    sub get_arg { my ($x) = shift =~ /="?([^="])/; $x =~ tr/"'//; $x; }

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://127933]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-03-19 06:12 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.