in reply to help with regex

The captures $1, $2, etc are number by their appearance in the pattern:
string=\"(\d+)\"|static-address\/(.*?)\" ^^^^^ ^^^^^ $1 $2

Replies are listed 'Best First'.
Re^2: help with regex
by Anonymous Monk on Mar 27, 2009 at 13:14 UTC
    So in other words I will need to check to see if $1 gets set otherwise use $2 correct? I was hoping I could set both to $1
      I guess I could do this:
      while(<DATA>) { chomp; if (/string=\"(\d+)\"|static-address\/(.*?)\"/) { my $id = $1 ? $1 : $2; print "ONE: $id\n"; } } __DATA__ static string="123456" containing numbers html value <a href="static-address/foo">text</a>
        The following would be better:
        if (/string=\"(\d+)\"|static-address\/(.*?)\"/) { my $id = defined($1) ? $1 : $2;

        And then there's this:

        if (/string=\"(\d+)\"/ || /static-address\/(.*?)\"/) { my $id = $1;