Project

General

Profile

Actions

Feature #5588

closed

add negation flag (v) to Regexp

Added by sunaku (Suraj Kurapati) over 12 years ago. Updated over 6 years ago.

Status:
Closed
Target version:
[ruby-core:40824]

Description

Please add a negation flag (v) to regexps which inverts them:

"ruby" =~ /perl/v #=> true (turn on negation)
"ruby" !~ /perl/v #=> false (turn on negation)
"ruby" =~ /(?v:perl)/ #=> true (turn on negation)
"ruby" !~ /(?v:perl)/ #=> false (turn on negation)
"ruby" =~ /(?-v:perl)/ #=> false (turn off negation)
"ruby" !~ /(?-v:perl)/ #=> true (turn off negation)

The flag name "v" comes from the ex(1) command of the same name.

This has beneficial applications where it is sometimes difficult
to construct what you want to match but much easier to construct
what you do not want to match. Having this negation built in
the regexp itself would remove the need for us to change our
Ruby code to process a regexp in a different way.

For example, suppose that you are passing a regexp to the --name
command-line option of MiniTest. This regexp tells MiniTest to
only run those tests whose names match. If Ruby had a negation
flag on its regexps, then it would be suddenly trivial to make
MiniTest skip certain tests by negating the regexp we pass in.

In this manner, we get a beneficial feature without ever changing
our Ruby code (the codebase of MiniTest in this example). :-)

Thanks for your consideration.


Files

5588_regexp_v.patch (15.7 KB) 5588_regexp_v.patch implementation done, but need to write RDoc sunaku (Suraj Kurapati), 11/13/2011 06:59 PM
0001-http-redmine.ruby-lang.org-issues-5588.patch (18.4 KB) 0001-http-redmine.ruby-lang.org-issues-5588.patch both implementation & RDoc done sunaku (Suraj Kurapati), 11/15/2011 04:55 AM
5588_negative_lookahead.patch (14.1 KB) 5588_negative_lookahead.patch sunaku (Suraj Kurapati), 11/16/2011 02:42 PM
5588_negative_lookahead.patch (14.3 KB) 5588_negative_lookahead.patch updated test case sunaku (Suraj Kurapati), 11/17/2011 07:50 AM

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11049: Enumerable#grep_v (inversed grep)Closedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0