On Feb 5, David Mamanakis said:

>I am building a parsing routine, and am using a regular expression, which
>works, EXCEPT when I need to EXCLUDE certain things...
>
>$right =~ s/A/X/g;
>
>However, I may need to exclude this replacement in some of the values of
>$right...
>
>Anything found between < and > SHOULD NOT be replaced.  \<.*\>
>Anything found between & and ; SHOULD NOT be replaced.  \&.*\;
>Anything found between {$ and } SHOULD NOT be replaced.  \{$.*\}
>Anything found between < and = SHOULD NOT be replaced.  \<.*\=

Here's a crafty trick (from DALnet #perl a couple of minutes ago)...

  $text =~ m{(<.*?>|&.*?;|{\$.*?}|<.*?=)|A}{$1 || "X"}seg;

Basically, if it's a special case, it's matched and put in $1, and if it's
"A", it's matched but $1 is undefined.  Then, on the right-hand side, we
use $1 if it has a value, and "X" otherwise.

The /s is so that . matches newlines, the /e is so that $1 || "X" is
evaluated as code, and the /g is for all matches.

It kinda sounds like you're working with HTML and some template thing,
though... you might want to use a full HTML parser instead.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to