Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

Re: Matching set of paragraph tags with string inside.

by jepri (Parson)
on Feb 08, 2008 at 21:11 UTC ( #667070=note: print w/ replies, xml ) Need Help??


in reply to Matching set of paragraph tags with string inside.

I try to avoid getting too tricky with regexes, because I am a bear of little brain. I'd break the string up then inspect it in pieces, perhaps like this:

my @arr = split /<\/p>/, $string; @matches = grep { /\[tab\]/ } @arr;

one extra line but much easier for me. If I was concerned about it looking neat, I'd put the code inside a subroutine called match_tabs().

___________________
Jeremy
I didn't believe in evil until I dated it.


Comment on Re: Matching set of paragraph tags with string inside.
Download Code
Re^2: Matching set of paragraph tags with string inside.
by the_0ne (Pilgrim) on Feb 08, 2008 at 21:26 UTC
    hmmm, actually makes sense. Luckily most of the time I will be looking exactly for what I originally showed in my example. This should work ok. I'll have to mess with it and see if there's anything I didn't think of that may pop up.

    Thanks.
Re^2: Matching set of paragraph tags with string inside.
by Roy Johnson (Monsignor) on Feb 08, 2008 at 21:26 UTC
    One minor difference is that splitting this way removes the close-paragraph, but not the open-paragraph, so you need to remove it.
    my @arr = grep {/\[tab\]/ and s/^<p>//} split /<\/p>/, $contents;
    If you wanted to retain both tags, you could do so by splitting on a lookbehind expression:
    my @arr = grep /\[tab\]/, split /(?<=<\/p>)/, $contents;
    You'd also retain the paragraph-close if you used it for $/ and read the string as a file.
    { local $/ = '</p>'; open (STR, '<', \$contents) or die "Opening string: $!\n"; @arr = grep /^<p>/ && /\[tab\]/, <STR>; print "read $_\n" for @arr; }
    But now I'm just getting silly.

    Caution: Contents may have been coded under pressure.
      You seem to be a bit focussed on using big regexes :P

      map { s/<P>// } grep { /\[tab\]/ } split /<\/p>/, $string;

      ___________________
      Jeremy
      I didn't believe in evil until I dated it.

        I think you should try this out. It doesn't do what you think it does.

        Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://667070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (16)
As of 2014-04-16 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls