Repeated captures from the same pattern and string

G

gorjusborg

I'm trying to match a string (which happens to be an entire file) as
many times as it can, and capture a portion to a variable.
The results I'm getting with the method I'm trying to use are..
unexpected.

Here is the code:

while ($text =~ m{$pattern1($pattern2)$pattern3\(}msg) {
push @instance, "$1";
}

I've shortened the patterns I'm using to place holders because they are
pretty complex, and I'm pretty sure they're working as I expect.

The problem is, though I'm getting the _number_ of matches I'd expect
(verified visually), I'm only getting the captured text from the first
match in $1, but repeated the number of times the pattern matches in
the text... What gives??

=Brandon=
 
D

David Squire

gorjusborg said:
I'm trying to match a string (which happens to be an entire file) as
many times as it can, and capture a portion to a variable.
The results I'm getting with the method I'm trying to use are..
unexpected.

Here is the code:

while ($text =~ m{$pattern1($pattern2)$pattern3\(}msg) {
push @instance, "$1";
}

I've shortened the patterns I'm using to place holders because they are
pretty complex, and I'm pretty sure they're working as I expect.

The problem is, though I'm getting the _number_ of matches I'd expect
(verified visually), I'm only getting the captured text from the first
match in $1, but repeated the number of times the pattern matches in
the text... What gives??

Could you please show us an example consisting of a small but *complete*
script including data and results, so we can see exactly how things are
"unexpected" and try it out for ourselves?

See the posting guidelines for this group, which are posted here twice
weekly. Following them will help you to help the group to help you.


DS
 
G

gorjusborg

Mirco said:
Thus spoke gorjusborg (on 2006-09-21 22:26):
while ($text =~ m{$pattern1($pattern2)$pattern3\(}msg) {
push @instance, "$1";
}
...
The problem is, though I'm getting the _number_ of matches I'd expect
(verified visually), I'm only getting the captured text from the first
match in $1, but repeated the number of times the pattern matches in
the text... What gives??


The code posted seems so far ok, which means your
error is somewhere else. As David pointed out,
please post some code that allows us to
replicate your problem.

I did run your (slightly modified, see "$1")
code against some random Nietzsche paragraph
in order to find out all non capitalized
words between two capitalized ones - and
your code indeed did work:

...
$text = do { local $/; <DATA> }; # pull some Nietzsche stuff ;-)

$pattern1 = '\b[A-Z][a-z]+\s+';
$pattern2 = '[a-z]+';
$pattern3 = '\s+[A-Z][a-z]+';

while( $text =~ m{$pattern1($pattern2)$pattern3}gms ) {
push @instance, $1;
}
print join "\n", @instance;
...

__DATA__
(put your favorite quote here)


Regards

Mirco

Thanks for the patience and heads-up on posting manners, as may be
obvious, I'm new to posting on useNet.

Mirco,
Thanks for help.
I'll look into it further, and post if the results are interesting.

=gorj=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top