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

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.