Forum Moderators: coopster & phranque

Message Too Old, No Replies

regex backreference array

$1 $2 $3 ... in an array?

         

physics

6:22 am on Apr 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm looking for an array of $1 and $2 and $3...
From what I understand @- holds the starting points of the matches in $-[0] and @+ holds the ending points. So from there I can get all of the matches. But is there a simpler way, i.e. a @? that holds all the regex matches? I want to loop over them and in this app one does not know ahead of time how many () there will be in the regex.

perl_diver

7:44 am on Apr 3, 2006 (gmt 0)

10+ Year Member



say you have:

$string = '1234 abcdef 123abc 123e 346 abcd 123abc 345 ghte 234gvd gbt345 6453 tty';

you want to get all the digit only sequences from the string:

@array_of_matches = $string =~ /\b(\d+)\b/g;

loop through @array_of_matches to see what you got:


for (@array_of_matches) {
print "$_\n";
}

$array_of_matches[0] is the first element in the array corresponding to the fist pattern match returned from the regexp: 1234

$array_of_matches[-1] is the last match: 6453

which is the same as:

$array_of_matches[$#array_of_matches]

maybe you can give a better idea of what you are trying to match and store in an array.

physics

5:19 am on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks perl_diver, that looks like just what I need.