Re: subpattern reference using vaiable subpattern index
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 30 Jan 2007 16:20:26 GMT
Mumia W. (NOSPAM) <paduille.4060.mumia.w+nospam@xxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
On 01/29/2007 09:17 AM, Yakov wrote:
I have submatch index in a variable, $SUB=5. How do I write submatch
reference $5 using $SUB ?
Is ${$SUB} ok ?
my $SUB = 5;
my @matches = m/.../;
print $matches[$SUB];
2. Is there array of subpatterns ($1..$<N>) so I can take it's size ?
Yakov
Yes, after you have assigned the output from the match operator to an array.
That's not always necessary. The system arrays @+ and @- also hold
(string pointers to) the submatches. See perlvar about them.
my @matches = m/.../;
print scalar(@matches);
....or
m/.../ and print @- - 1, "\n";
The array @- longer by one than @matches would be because $-[0]
holds the beginning of the entire match (captured or not). Hence
the subtraction of one.
On the other hand, with a //g modifier your list assignment would
collect all the captures in the global match while @- reflects only
the state of the last match.
Anno
.
- References:
- subpattern reference using vaiable subpattern index
- From: Yakov
- Re: subpattern reference using vaiable subpattern index
- From: Mumia W. (NOSPAM)
- subpattern reference using vaiable subpattern index
- Prev by Date: Re: Statistics Extraction
- Next by Date: Regex repeating capture
- Previous by thread: Re: subpattern reference using vaiable subpattern index
- Next by thread: French Accents appear incorrectly...
- Index(es):
Relevant Pages
|