in reply to simple regex question

$_ = 'password=foo&user=bar'; print $1 if /password=(.+?)&/;


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: simple regex question
by sk (Curate) on Jul 16, 2005 at 18:32 UTC
    This is nothing to do with the original question as such. Just curious. To print 'foo' we can just use this right?  perl -le '$_ = "password=foo&user=bar"; print /password=(.+?)&/;'

    output: foo

    by the same token, am i correct in assuming if we were setting $1, $2 etc.  /regexstuff/ will return $1 . $2?

    thanks

    SK

      am i correct in assuming if we were setting $1, $2 etc. /regexstuff/ will return $1 . $2?

      No, but why guess? Here's the relevant section of perlop:

      m/PATTERN/cgimosx
      /PATTERN/cgimosx

      Searches a string for a pattern match, and in scalar context returns true if it succeeds, false if it fails.

      ...

      If the "/g" option is not used, "m//" in list con­text returns a list consisting of the subexpres­sions matched by the parentheses in the pattern, i.e., ($1, $2, $3...). ... When there are no parentheses in the pattern, the return value is the list "(1)" for success. With or without parentheses, an empty list is returned upon failure.

      the lowliest monk