FAQ
Greetings.

I'm matching a pattern against a long string, and I want to examine each
occurrence of the match. I know that $& stores the match, but is there
a way to store EVERY match in the string, and then look at them?

Example:

$str = "This is a very long string, isn't it?"

If I want to match against something like /i[st]/ to pick up all the
'is' and 'it' strings, then see what I've got (i.e. each instance), can
I do it?

I've tried to wade through the whole \G /g etc. world, but it doesn't
seem to work for me.

Thanks!

Dan T.
planted@nc.rr.com

Search Discussions

  • Jon Molin at Mar 6, 2002 at 10:11 am

    "Daniel J. Tomso" wrote:

    Greetings.

    I'm matching a pattern against a long string, and I want to examine each
    occurrence of the match. I know that $& stores the match, but is there
    a way to store EVERY match in the string, and then look at them?

    Example:

    $str = "This is a very long string, isn't it?"

    If I want to match against something like /i[st]/ to pick up all the
    'is' and 'it' strings, then see what I've got (i.e. each instance), can
    I do it?
    is this what you're after:

    $str = "This is a very long string, isn't it?"
    foreach ($str =~ /(i[is])/g)
    {
    do stuff
    }

    or collect them all:

    $str = "This is a very long string, isn't it?"
    my @store = ($str =~ /(i[is])/g);

    I'm not sure this is what you wanted...
    /Jon
    I've tried to wade through the whole \G /g etc. world, but it doesn't
    seem to work for me.

    Thanks!

    Dan T.
    planted@nc.rr.com

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Jenda Krynicky at Mar 6, 2002 at 10:31 am
    From: "Daniel J. Tomso" <tomso@niehs.nih.gov>
    I'm matching a pattern against a long string, and I want to examine
    each occurrence of the match. I know that $& stores the match, but is
    there a way to store EVERY match in the string, and then look at them?
    Dont use $&, it'll slow down ALL regexps even if you use it once!
    Example:

    $str = "This is a very long string, isn't it?"

    If I want to match against something like /i[st]/ to pick up all the
    'is' and 'it' strings, then see what I've got (i.e. each instance),
    can I do it?
    Either

    @matches = ($str =~ /(i[st])/g);

    or

    while ($str =~ /(i[st])/g) {
    my $match = $1;
    ...
    }

    Jenda

    =========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
    There is a reason for living. There must be. I've seen it somewhere.
    It's just that in the mess on my table ... and in my brain.
    I can't find it.
    --- me

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 5, '02 at 11:28p
activeMar 6, '02 at 10:31a
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase