in reply to Re: Regex help
in thread Regex help

Silly me, I forgot what the problem was and mistated it. The problem is that the string I am searching for can exist as a substring of another element of @contexts, but I don't want it to match if it's already been rewritten.

For example in the list I am looping through, one element could be demo-outgoing, and a later element could be demo. I don't want the string demo in the already formatted string __user__demo-outgoing__ to be matched and rewritten again.

Does that make sense?

Replies are listed 'Best First'.
Re^3: Regex help
by ikegami (Patriarch) on Jun 02, 2005 at 18:11 UTC

    So @context contains search strings? (This is why it's important to give a runable piece of code, including sample input. It takes the guessing out of the game.)

    my @contexts = qw( demo-outgoing demo ); local $_ = 'foo demo-outgoing bar'; my $regexp = join('|', map { quotemeta($_) } @contexts); s/($regexp)/__user__${1}__/g; print("$_\n"); # foo __user__demo-outgoing__ bar