Krab Krab - 1 year ago 61
Perl Question

How do I replace all occurrences of certain characters with their predecessors?

$s = "bla..bla";
$s =~ s/([^%])\./$1/g;

I think it should replace all occurrences of
that is not after
with the character that is before

is then:
, but
it should be
. Where is the problem? I know I can use quantifiers, but I need do it this way.

Answer Source

When a global regular expression is searching a string it will not find overlapping matches.

The first match in your string will be a., which is replaced with a. When the regex engine resumes searching it starts at the next . so it sees .bla as the rest of the string, and your regex requires a character to match before the . so it cannot match again.

Instead, use a negative lookbehind to perform the assertion that the previous character is not %:

$s =~ s/(?<!%)\.//g;

Note that if you use a positive lookbehind like (?<=[^%]), you will not replace the . if it is the first character in the string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download