Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Skipping special tags in regexes

by Abigail-II (Bishop)
on Dec 04, 2003 at 16:09 UTC ( [id://312233]=note: print w/replies, xml ) Need Help??


in reply to Skipping special tags in regexes

#!/usr/bin/perl use strict; use warnings; my $tag = '(?:<[^>]*>)'; sub word { my $word = shift; qr /$tag*$word/; } while (<DATA>) { s/((??{ word 'tacos' }))/yummy $1/g; s/((??{ word 'salad' }))/green $1/g; print; } __DATA__ I like tacos. <5b>I <5c>like <5d>tacos. I like a salad. <foo>I <foo>like <foo>a <bar><baz><foo>salad. <foo>I <foo>like <foo>a <bar><baz><foo>salad <bup>with <bobob>tacos. I like yummy tacos. <5b>I <5c>like yummy <5d>tacos. I like a green salad. <foo>I <foo>like <foo>a green <bar><baz><foo>salad. <foo>I <foo>like <foo>a green <bar><baz><foo>salad <bup>with yummy <bo +bob>tacos.

Abigail

Replies are listed 'Best First'.
Re: Re: Skipping special tags in regexes
by flounder99 (Friar) on Dec 04, 2003 at 18:40 UTC
    Is there a reason you are using the /(??{ })/ code block other than just to make a more general solution? This seems to work just fine.
    #!/usr/bin/perl use strict; use warnings; my $tag = '(?:<[^>]*>)'; while (<DATA>) { s/($tag*tacos)/yummy $1/g; s/($tag*salad)/green $1/g; print; } __DATA__ I like tacos. <5b>I <5c>like <5d>tacos. I like a salad. <foo>I <foo>like <foo>a <bar><baz><foo>salad. <foo>I <foo>like <foo>a <bar><baz><foo>salad <bup>with <bobob>tacos. I like yummy tacos. <5b>I <5c>like yummy <5d>tacos. I like a green salad. <foo>I <foo>like <foo>a green <bar><baz><foo>salad. <foo>I <foo>like <foo>a green <bar><baz><foo>salad <bup>with yummy <bo +bob>tacos.

    --

    flounder

      Is there a reason you are using the /(??{ })/ code block other than just to make a more general solution?
      Because I was expecting the OP to refine his question, and come up with a slightly different definition of a "word" (perhaps it needed trailing tags as well, or not more than 2 tags, whatever). Then I only need to change the sub, and not every regex using it.

      I think my solution is more general than yours, but the effect is the same.

      Abigail

        I assumed you were shooting for generality. I just wanted to know if there was a technical reason I was missing.

        --

        flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found