in reply to Re: Regex Question
in thread Regex Question

You were bitten by negative-over-or-logic. The left side of the | will match what the right side is intended to skip. It has to be something like
$field =~ s/ (?<!C(?=.O)) # Look back a character and make sure it's not C-some +thing-O from there (?<!\d(?=.\d)) # Make sure it's also not a digit-something-digit (?:\\|\/) # The something is a slash or backslash / & /xgi;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3: Regex Question
by Aristotle (Chancellor) on Nov 15, 2005 at 19:58 UTC

    That won’t match the exact same things. The OP’s regex will replace the slash in "/2" or in "1/", yours won’t. Not sure if this is part of the spec, but it’s something to be aware of.

    Meh. That’s it, I’m going back to bed.

    Makeshifts last the longest.

      Mine does replace the slash when it has a digit on only one side. The logic is that the slash is replaced unless there is a digit on both sides. It is the OP's logic that will not replace a slash with a digit on either side.
      for my $field ( map $_, 'C/O', '2/4', 'C/2', 'd/l', '2/', '/2') { print "$field..."; $field =~ s/ (?<!C(?=.O)) # Look back a character and make sure it's not C-so +mething-O from there (?<!\d(?=.\d)) # Make sure it's also not a digit-something-digit (?:\\|\/) # The something is a slash or backslash / & /xgi; print "$field\n"; }

      Caution: Contents may have been coded under pressure.
      Yes, that would comply with my specification of the problem. If there is only a digit on one side, replace the slash with an ampersand.
      I use the most powerful debugger available: print!