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