in reply to [OT] Thoughts on Ruby's new absent operator?
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: [OT] Thoughts on Ruby's new absent operator?
by LanX (Saint) on Mar 24, 2017 at 16:59 UTC | |
|
Re^2: [OT] Thoughts on Ruby's new absent operator?
by Anonymous Monk on Mar 24, 2017 at 15:19 UTC | |
by perlancar (Hermit) on Mar 25, 2017 at 02:55 UTC |