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!


In reply to Re^2: Regex for replacing a character "not next to" another character (updated) by LanX
in thread Regex for replacing a character "not next to" another character by flightdm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.