Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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"

Thanks

davorg: expanded node title

Comment on Help with regexp to capture quotes
Select or Download Code
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.

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!

      TIMTOWTDI
      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"



      HTH,
      Charles K. Clarkson


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

      Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://127933]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-09-23 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls