in reply to Re: Pattern Matching
in thread Pattern Matching

Thanks, thats what I already had, but those only seem to get the first occurance... then adds the rest of the page after it. Almost like the second part of the regex isn't telling it when to stop...

Here is the pattern i'm trying to match, have I got the regex wrong?

some random text<B><P ALIGN=CENTER>My Text I want to grab 1</B></FONT> +<p>some of stuff<B><P ALIGN=CENTER>My Text I want to grab 2</B></FONT +> some other random text<B><P ALIGN=CENTER>My Text I want to grab 3</ +B></FONT>

Any ideas?

Thanks again

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Replies are listed 'Best First'.
Re^3: Pattern Matching
by Roy Johnson (Monsignor) on Mar 09, 2005 at 13:44 UTC
    $_ = 'some random text<B><P ALIGN=CENTER>My Text I want to grab 1</B>< +/FONT><p>some of stuff<B><P ALIGN=CENTER>My Text I want to grab 2</B> +</FONT> some other random text<B><P ALIGN=CENTER>My Text I want to gr +ab 3</B></FONT>'; my @wants = /<B><P ALIGN\=CENTER>(.+?)<\/B><\/FONT>/g; print "$_\n" for @wants;
    Note non-greedy match.

    Caution: Contents may have been coded under pressure.
Re^3: Pattern Matching
by ww (Archbishop) on Mar 09, 2005 at 16:14 UTC
    Just for the record, that's ill-formed html in your example:
    <B><P ALIGN=CENTER>My Text I want to grab 1</B></FONT><p>
    most of the issues will be ignored by most browsers, but are you certain? If displayed !eq actual, regex will have a hard time

    (and, OT: writing 4.01 compliant code and using css IS worth the trouble -- for anything you're putting online, anyway)

    Directly on your question: Note that your pattern is greedy, (.+), aka one-or-more-of-anything_except_a_newline. As written, $line =~ m/<B><P ALIGN\=CENTER>(.+)<\/B><\/FONT>/g; that should swallow everything UP TO the last end_bold_new_para tags.

    And re capturing all instances: suggest you push to an array inside a foreach loop. The array subscript will be incremented for each new item found.