Repeated captures from the same pattern and string

Discussion in 'Perl Misc' started by gorjusborg, Sep 21, 2006.

  1. gorjusborg

    gorjusborg Guest

    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=
    gorjusborg, Sep 21, 2006
    #1
    1. Advertising

  2. gorjusborg

    David Squire Guest

    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
    David Squire, Sep 21, 2006
    #2
    1. Advertising

  3. gorjusborg

    gorjusborg Guest

    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=
    gorjusborg, Sep 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall

    Screen captures

    Joshua Beall, Jan 19, 2004, in forum: HTML
    Replies:
    4
    Views:
    442
    Toby A Inkster
    Jan 19, 2004
  2. El Gato

    Regexp captures

    El Gato, Jan 30, 2007, in forum: Ruby
    Replies:
    2
    Views:
    85
    El Gato
    Jan 31, 2007
  3. Ari Brown

    Regexp: named captures

    Ari Brown, Aug 20, 2007, in forum: Ruby
    Replies:
    20
    Views:
    310
    Steve Austen
    Nov 29, 2010
  4. Markus Fischer
    Replies:
    9
    Views:
    161
    7stud --
    Apr 8, 2011
  5. Bill

    regex @a = m / | /g and captures?

    Bill, Oct 17, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    114
Loading...

Share This Page