mlightner-enhanced_regexp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +47 -0
- data/lib/enhanced_regexp/inversion.rb +52 -0
- data/lib/enhanced_regexp/options_modification.rb +69 -0
- data/lib/enhanced_regexp.rb +7 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Enhanced Regular Expressions
|
2
|
+
============================
|
3
|
+
Author: mlightner@gmail.com
|
4
|
+
|
5
|
+
This package extends the functionality of Regexp objects by adding methods to create copies of them
|
6
|
+
with modified options. It also allows for toggling of "inverted" status on a regexp object.
|
7
|
+
|
8
|
+
Example:
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'enhanced_regexp'
|
12
|
+
irb> exp = /test/i
|
13
|
+
=> /test/i
|
14
|
+
irb> exp.case_sensitive?
|
15
|
+
=> true
|
16
|
+
irb> exp2 = exp.without_case
|
17
|
+
=> /test/
|
18
|
+
irb> exp2.case_sensitive?
|
19
|
+
=> false
|
20
|
+
irb> mlx = /test.*multiline/
|
21
|
+
=> /test.*multiline/
|
22
|
+
irb> "test\n multiline".match(mlx)
|
23
|
+
=> nil
|
24
|
+
irb> "test\n multiline".match(mlx.with_multiline)
|
25
|
+
=> #<MatchData:0xb7c0c004>
|
26
|
+
irb> "test\n multiline".match(mlx.with_multiline).inspect
|
27
|
+
=> "#<MatchData:0xb7c0725c>"
|
28
|
+
irb> invx = /hello/i.invert!
|
29
|
+
=> /hello/i
|
30
|
+
irb> invx.inverted?
|
31
|
+
=> true
|
32
|
+
irb> "oh hello".match(invx)
|
33
|
+
=> false
|
34
|
+
irb> "oh hai".match(invx)
|
35
|
+
=> true
|
36
|
+
irb> invx.uninvert!
|
37
|
+
=> /hello/i
|
38
|
+
irb> "oh hello".match(invx)
|
39
|
+
=> #<MatchData:0xb7bedac8>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
NOTES:
|
44
|
+
- With inversion, you can toggle it on any given regular expression without creating a new one.
|
45
|
+
- When changing other option values, a new Regexp will ge generated and returned as the value of
|
46
|
+
the call to regex.with_option (or without_option). This new regular expression will be identical
|
47
|
+
to the original aside from the requested option change.
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module MattLightner
|
2
|
+
|
3
|
+
module EnhancedRegexp
|
4
|
+
|
5
|
+
#= Inverted Regular Expressions
|
6
|
+
# Gives the ability to invert a regular expression so that running a match against it
|
7
|
+
# will yield a true result when it does NOT match the target string.
|
8
|
+
module Inversion
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.send(:include, MattLightner::EnhancedRegexp::Inversion::InstanceMethods)
|
12
|
+
base.class_eval do
|
13
|
+
alias_method :match_without_inversion, :match
|
14
|
+
alias_method :match, :match_with_inversion
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
# Invert this regular expression.
|
20
|
+
def invert!
|
21
|
+
@inverted = true
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
# Uninvert this regular expression.
|
26
|
+
def uninvert!
|
27
|
+
@inverted = false
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_inverted(value = true)
|
32
|
+
@inverted = (value) ? true : false
|
33
|
+
end
|
34
|
+
|
35
|
+
# Is this an inverted regular expression?
|
36
|
+
def inverted?
|
37
|
+
@inverted rescue false
|
38
|
+
end
|
39
|
+
|
40
|
+
def match_with_inversion(*args, &block)
|
41
|
+
result = match_without_inversion(*args, &block)
|
42
|
+
if @inverted
|
43
|
+
result.nil? ? true : false
|
44
|
+
else
|
45
|
+
match_without_inversion(*args, &block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module MattLightner
|
2
|
+
|
3
|
+
module EnhancedRegexp
|
4
|
+
|
5
|
+
#= Options Modification
|
6
|
+
# Makes it easy to modify options set on a regular expression.
|
7
|
+
module OptionsModification
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send(:include, MattLightner::EnhancedRegexp::OptionsModification::InstanceMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
|
15
|
+
#== Case sensitivity: the IGNORECASE flag
|
16
|
+
# Return a new regular expression identical to this one except without case sensitivity.
|
17
|
+
def without_case
|
18
|
+
Regexp.new(self.source, (self.options ^ Regexp::IGNORECASE)).set_inverted(self.inverted?)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Return a new regular expression identical to this one except with case sensitivity.
|
22
|
+
def with_case
|
23
|
+
Regexp.new(self.source, (self.options | Regexp::IGNORECASE)).set_inverted(self.inverted?)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Boolean test to see if this regular expression is case sensitive.
|
27
|
+
def case_sensitive?
|
28
|
+
(self.options | Regexp::IGNORECASE) == self.options
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
#== Multi-line matching: the MULTILINE flag
|
33
|
+
# Return a new regular expression identical to this one except without case sensitivity.
|
34
|
+
def without_multiline
|
35
|
+
Regexp.new(self.source, (self.options ^ Regexp::MULTILINE)).set_inverted(self.inverted?)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Return a new regular expression identical to this one except with case sensitivity.
|
39
|
+
def with_multiline
|
40
|
+
Regexp.new(self.source, (self.options | Regexp::MULTILINE)).set_inverted(self.inverted?)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Boolean test to see if this regular expression is case sensitive.
|
44
|
+
def multiline?
|
45
|
+
(self.options | Regexp::MULTILINE) == self.options
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
#== Multi-line matching: the EXTENDED flag
|
50
|
+
# Return a new regular expression identical to this one except without case sensitivity.
|
51
|
+
def without_extended
|
52
|
+
Regexp.new(self.source, (self.options ^ Regexp::EXTENDED)).set_inverted(self.inverted?)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return a new regular expression identical to this one except with case sensitivity.
|
56
|
+
def with_extended
|
57
|
+
Regexp.new(self.source, (self.options | Regexp::EXTENDED)).set_inverted(self.inverted?)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Boolean test to see if this regular expression is case sensitive.
|
61
|
+
def extended?
|
62
|
+
(self.options | Regexp::EXTENDED) == self.options
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mlightner-enhanced_regexp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Lightner
|
8
|
+
autorequire: name
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mlightner@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/enhanced_regexp.rb
|
26
|
+
- lib/enhanced_regexp/inversion.rb
|
27
|
+
- lib/enhanced_regexp/options_modification.rb
|
28
|
+
- README
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://mattlightner.com/code/enhanced_regexp
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: Enhanced regular expressions allow for a boolean inverted/not inverted association to be attached to any regular expression, as well as easy modification of Regexp options (e, i, m).
|
55
|
+
test_files: []
|
56
|
+
|