Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Repeated captures from the same pattern and string

Reply
Thread Tools

Repeated captures from the same pattern and string

 
 
gorjusborg
Guest
Posts: n/a
 
      09-21-2006
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=

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      09-21-2006
gorjusborg wrote:
> 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
 
Reply With Quote
 
 
 
 
gorjusborg
Guest
Posts: n/a
 
      09-22-2006
Mirco Wahab wrote:
> 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=

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plug-in USB hardware device captures keystrokes on Mac and PC USB keyboards. Theo Computer Security 15 12-06-2012 02:10 AM
Match a pattern multiple times, returning matches, captures andoffset? Markus Fischer Ruby 9 04-08-2011 07:53 PM
How to use Regex to breakdown a pattern and use the pattern to breakdown a string ChrisC Perl Misc 4 06-25-2010 05:46 PM
Best hardware and or Software for Screen Video Captures in games ? Paul GoodBar DVD Video 0 05-10-2005 03:11 AM
DVD captures with Hypersnap and PowerDVD. Is there anything else? Patrick D. Rockwell DVD Video 4 07-23-2004 02:14 AM



Advertisments