in reply to Extract substring from string with no whitespace using regexp?

Use $1, $2 etc.

After a match, $1 will contain the part of the match in the first parentheses, and $2 will contain the part of the match in the second parentheses and so on.

Cheers!

  • Comment on Re: Extract substring from string with no whitespace using regexp?

Replies are listed 'Best First'.
Re: Re: Extract substring from string with no whitespace using regexp?
by ysth (Canon) on Feb 26, 2004 at 04:27 UTC
    ...where the parentheses are numbered by the position of the left parentheses, so "abc" =~ /((a)b(c))/ will set $1 to "abc", $2 to "a", and $3 to "c".

    If you use a regex that has parentheses in list context, the substrings are returned in a list, and can be used directly:

    my ($abc, $a, $c) = "abc" =~ /((a)b(c))/

    see perlop for more information about m// and s/// and what they return and how flags like //g affect them.