in reply to Re: Regex for replacing a character "not next to" another character
in thread Regex for replacing a character "not next to" another character

Hi

Probably I still need more coffee ... ;)

... but shouldn't this  look-behind assertion

>  (?<=[^"])

rather be negated?

 (?<![^"])

edit

Never mind.

Indeed not enough coffee, you are mixing two approaches which was confusing me.

UPDATE

there is a limitation in your approach when dealing with multiple lines. (though the OP didn't explicitly ask for this)

DB<135> $str = "\tstart\tmiddle1\t\"quote1\tquote2\"\tmiddle2\tend\t +"; => "\tstart\tmiddle1\t\"quote1\tquote2\"\tmiddle2\tend\t" DB<136> $str .= "\n$str" => "\tstart\tmiddle1\t\"quote1\tquote2\"\tmiddle2\tend\t\n\tstart\tmi +ddle1\t\"quote1\tquote2\"\tmiddle2\tend\t" DB<137> p $str start middle1 "quote1 quote2" middle2 end start middle1 "quote1 quote2" middle2 end => 1 DB<138> p $str =~ s/ (?<=[^"]) \t (?!"|$) /***/gmxr start***middle1 "quote1***quote2" middle2***end ***start***middle1 "quote1***quote2" middle2***end => 1 DB<139> p $str =~ s/ (?<!")(?<!^) \t (?!"|$) /***/gmxr start***middle1 "quote1***quote2" middle2***end start***middle1 "quote1***quote2" middle2***end => 1

You filter tabs "preceded by any character which isn't a quote" with (?<=[^"]) supposing that line-start is not any character.

As you can see BUK's approach still works in this case.

FWIW:

It was first confusing me that (?!"|^) wasn't used, but the regex engine rejects "variable length look-behind assertions" (which is not really the case here)

DB<140> p $str =~ s/ (?<!"|^) \t (?!"|$) /***/gmxr Variable length lookbehind not implemented in regex m/ (?<!"|^) \t (?! +"|$) / at (eval 110)[multi_perl5db.pl:644] line 2.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^3: Regex for replacing a character "not next to" another character (updated)
by tybalt89 (Monsignor) on Nov 04, 2016 at 11:00 UTC

    For a multi-line string, simply add \n to the negated character group:

    s/(?<=[^"\n])\t(?!"|$)//gm

    and of course a /m for the $

      OK, but in this case, for the sake of readability and maintainability, better stick with one approach
      DB<105> p $str =~ s/ (?<=[^"\n]) \t (?=[^"\n]) /***/gxr start***middle1 "quote1***quote2" middle2***end start***middle1 "quote1***quote2" middle2***end

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!