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
Probably I still need more coffee ... ;)
... but shouldn't this look-behind assertion
> (?<=[^"])
rather be negated?
(?<![^"])
Never mind.
Indeed not enough coffee, you are mixing two approaches which was confusing me.
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.
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 | |
by LanX (Saint) on Nov 04, 2016 at 11:36 UTC |