in reply to [OT] Thoughts on Ruby's new absent operator?
Although it's fairly simple to translate the operator into perl, I don't see a way that does not involve repeating the contained expression: /A(?~BCD)E/ would match the same thing as /A(?: (?!BCD).* | (?:BCD).+ )E/x (ie: BCD is allowed to match between A and E if the string is not strictly BCD)
This looks like an easy way to exclude some values from a match precisely and explicitly. One example I can think of is if you have emails with the pattern firstnamesurname@company.pl and want to match last names in @lastnames, but not first names in @firstnames, you could do:
$" = |; /^(?~@firstnames)(?:@lastnames)[@]company[.]pl/;
without having to care if one of the matching names starts like one of the ignored ones (eg: benjamin and ben). I don't see how you'd end up searching for something like that though :P
Edit: even (?: (?!REGEX).* | (?:REGEX).+ ) can't be expected to always give the same result as (?~REGEX).
Eg: ABC =~ /A(?~BC)BC/ would be true (because the empty string is not BC), but ABC =~ /A(?: (?!BC).* | (?:BC).+ ) )BC/x; would be false because both branches fail. (?: (?!REGEX).+ | (?:REGEX).+ )? would work I think.
Edit2: even the latter would not be correct with (?~REGEX|) (ie, if the empty string is not allowed). So there actually is no way to turn (?~REGEX) into a perl equivalent without understanding what REGEX matches (Edit: well, I can't think of one right now at least).
|
|---|