Class: Verbal
- Inherits:
-
Regexp
- Object
- Regexp
- Verbal
- Defined in:
- lib/verbal.rb
Overview
Ruby Verbal Expressions, based on the awesome JavaScript repo by @jehna.
Instance Method Summary collapse
-
#any_of(value) ⇒ Object
(also: #any)
Matches any one of the characters in
value
. -
#anything ⇒ Object
Matches any character any number of times.
-
#anything_but(value) ⇒ Object
Matches any number of any character that is not in
value
. -
#capture(&block) ⇒ Object
Captures the nested regular expression.
-
#end_of_line ⇒ Object
Marks the expression to end at the last character of a line.
-
#end_of_string ⇒ Object
Marks the expression to start at the end of the string.
-
#find(value) ⇒ Object
Matches an exact string.
-
#initialize(&block) ⇒ Verbal
constructor
A new instance of Verbal.
-
#line_break ⇒ Object
(also: #br)
Adds a universal line break expression.
-
#maybe(value) ⇒ Object
Add a string to the expression that might appear once.
-
#multiple(value) ⇒ Object
Matches one or many of value.
-
#otherwise(value = nil) ⇒ Object
Adds a alternative expression to be matched.
-
#range(*args) ⇒ Object
Matches a character from the given range.
-
#start_of_line ⇒ Object
Marks the expression to start at the beginning of a line.
-
#start_of_string ⇒ Object
Marks the expression to start at the beginning of the string.
-
#tab ⇒ Object
Matches the tab character.
-
#word ⇒ Object
Matches a word, which is a continuous chunk of any alphanumeric character.
Constructor Details
#initialize(&block) ⇒ Verbal
Returns a new instance of Verbal.
13 14 15 16 17 18 19 20 |
# File 'lib/verbal.rb', line 13 def initialize(&block) @prefixes = '' @source = '' @suffixes = '' @modifiers = '' # TODO: Ruby Regexp option flags instance_eval(&block) super(@prefixes + @source + @suffixes, @modifiers) end |
Instance Method Details
#any_of(value) ⇒ Object Also known as: any
Matches any one of the characters in value
.
146 147 148 |
# File 'lib/verbal.rb', line 146 def any_of(value) append "[#{sanitize value}]" end |
#anything ⇒ Object
Matches any character any number of times.
98 99 100 |
# File 'lib/verbal.rb', line 98 def anything append '(?:.*)' end |
#anything_but(value) ⇒ Object
Matches any number of any character that is not in value
.
108 109 110 |
# File 'lib/verbal.rb', line 108 def anything_but(value) append "(?:[^#{sanitize value}]*)" end |
#capture(&block) ⇒ Object
Captures the nested regular expression.
211 212 213 |
# File 'lib/verbal.rb', line 211 def capture(&block) append "(#{Verbal.new(&block).source})" end |
#end_of_line ⇒ Object
Marks the expression to end at the last character of a line.
52 53 54 |
# File 'lib/verbal.rb', line 52 def end_of_line @suffixes = '$' end |
#end_of_string ⇒ Object
Marks the expression to start at the end of the string.
78 79 80 |
# File 'lib/verbal.rb', line 78 def end_of_string @suffixes += '\z' end |
#find(value) ⇒ Object
Matches an exact string.
30 31 32 |
# File 'lib/verbal.rb', line 30 def find(value) append "(?:#{sanitize value})" end |
#line_break ⇒ Object Also known as: br
Adds a universal line break expression.
119 120 121 |
# File 'lib/verbal.rb', line 119 def line_break append '(?:\n|(?:\r\n))' end |
#maybe(value) ⇒ Object
Add a string to the expression that might appear once.
89 90 91 |
# File 'lib/verbal.rb', line 89 def maybe(value) append "(?:#{sanitize value})?" end |
#multiple(value) ⇒ Object
Matches one or many of value.
176 177 178 179 180 181 |
# File 'lib/verbal.rb', line 176 def multiple(value) append case value when Regexp then "(#{value.source})+" else "(#{sanitize value})+" end end |
#otherwise(value = nil) ⇒ Object
Adds a alternative expression to be matched.
194 195 196 197 198 199 |
# File 'lib/verbal.rb', line 194 def otherwise(value = nil) @prefixes += "(?:" @suffixes = ")" + @suffixes append(")|(?:") find(value) if value end |
#range(*args) ⇒ Object
Matches a character from the given range.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/verbal.rb', line 157 def range(*args) value = "[" args.each_slice(2) do |from, to| from = sanitize(from) to = sanitize(to) value += "#{from}-#{to}" end value += "]" append value end |
#start_of_line ⇒ Object
Marks the expression to start at the beginning of a line.
41 42 43 |
# File 'lib/verbal.rb', line 41 def start_of_line @prefixes = '^' end |
#start_of_string ⇒ Object
Marks the expression to start at the beginning of the string.
65 66 67 |
# File 'lib/verbal.rb', line 65 def start_of_string @prefixes = '\A' + @prefixes end |
#tab ⇒ Object
Matches the tab character.
127 128 129 |
# File 'lib/verbal.rb', line 127 def tab append '\t' end |
#word ⇒ Object
Matches a word, which is a continuous chunk of any alphanumeric character.
135 136 137 |
# File 'lib/verbal.rb', line 135 def word append '\w+' end |