It would probably be better to link to the article itself. Anyway, I get the idea that (?~abc) would be analogous to [^x], except that the former is for multi-character sequences. I assume that similar effects can be achieved in Perl with Lookaround Assertions. What I'm missing at the moment are some more examples of practical applications - the only one mentioned in the article and the Ruby docs is not matching invalid C comments (second final example below).
An excerpt from the Ruby docs:
(?~subexp) absent operator (experimental)
Matches any string which doesn't contain any string which matches subexp. Similar to (?:(?!subexp).)*, but easy to write.
Unlike (?:(?!abc).)*c, (?~abc)c matches "abc", because (?~abc) matches "ab".
A sandbox for finding equivalent expressions:
use warnings; use strict; use Test::More; # in Ruby: (?~abc) my $re1 = qr{ \A (?: (?!abc) . )* \z }x; like '', $re1; like 'ab', $re1; like 'aab', $re1; like 'ccdd', $re1; unlike 'abc', $re1; unlike 'aabc', $re1; unlike 'ccccabc', $re1; unlike 'ccabcdd', $re1; # in Ruby: (?~abc)c # this example fails in Perl like 'abc', qr{ \A (?: (?!abc) . )* c \z }x; # in Ruby: \A\/\*(?~\*\/)\*\/\z my $re2 = qr{ \A /\* ( (?!\*/). )*? \*/ \z }x; like '/**/', $re2; like '/* foobar */', $re2; unlike '/**/ */', $re2; done_testing;
In reply to Re: [OT] Thoughts on Ruby's new absent operator?
by haukex
in thread [OT] Thoughts on Ruby's new absent operator?
by perlancar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |