Class: Verbal

Inherits:
Regexp
  • Object
show all
Defined in:
lib/verbal.rb

Overview

Ruby Verbal Expressions, based on the awesome JavaScript repo by @jehna.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Verbal

Returns a new instance of Verbal.

Examples:

Create a new RegExp

verbal = Verbal.new do
  start_of_line
  find 'x'
end
verbal =~ 'x' # => 0


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.

Examples:

Find one of ‘abc’ in ‘xkcd’

verbal = Verbal.new do
  any_of 'abc'
end
'xkcd'.scan verbal # => ['c']

Parameters:

  • value (String)

    string of characters to match



146
147
148
# File 'lib/verbal.rb', line 146

def any_of(value)
  append "[#{sanitize value}]"
end

#anythingObject

Matches any character any number of times.

Examples:

Match the entire string.

Verbal.new do
  anything
end


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.

Examples:

Match everything except underscores.

Verbal.new do
  anything_but '_'
end

Parameters:

  • value (String)

    characters to excluded



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.

Examples:

Capture the title of the concert and performer

verbal = Verbal.new do
  capture { anything }
  find /\sby\s/
  capture { anything }
end
data = verbal.match('this is it by michael jackson')
data[1] # => 'this is it'
data[2] # => 'michael jackson'


211
212
213
# File 'lib/verbal.rb', line 211

def capture(&block)
  append "(#{Verbal.new(&block).source})"
end

#end_of_lineObject

Marks the expression to end at the last character of a line.

Examples:

Add a hyphen to the end of each line.

lines = "first\nsecond\nthird"
verbal = Verbal.new do
  end_of_line
end
lines.gsub(verbal, '- ') # => "first- \nsecond- \nthird"


52
53
54
# File 'lib/verbal.rb', line 52

def end_of_line
  @suffixes = '$'
end

#end_of_stringObject

Marks the expression to start at the end of the string.

Examples:

Matching the entire string

verbal = Verbal.new do
  start_of_string
  find 'dinosaur'
  end_of_string
end
verbal.match('dinosaur')  # matches
verbal.match('dinosaurs') # does not match


78
79
80
# File 'lib/verbal.rb', line 78

def end_of_string
  @suffixes += '\z'
end

#find(value) ⇒ Object

Matches an exact string.

Examples:

Replace all dots with the word “Stop”.

paragraph = "Lorem. Dolor."
verbal    = Verbal.new do
  find '.'
end
paragraph.gsub(verbal, 'Stop') # => "Lorem Stop Dolor Stop"

Parameters:

  • value (String)

    string to match



30
31
32
# File 'lib/verbal.rb', line 30

def find(value)
  append "(?:#{sanitize value})"
end

#line_breakObject Also known as: br

Adds a universal line break expression.

Examples:

Converts all line breaks to unix-style with <br> tag.

lorem = "Lorem.\r\nDolor\namet."
verbal = Verbal.new do
  line_break
end
lorem.gsub(veral, "<br>\n") # => "Lorem.<br>\nDolor<br>\namet."


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.

Examples:

Find http or https.

Verbal.new do
  find 'http'
  maybe 's'
end

Parameters:

  • value (String)

    the string to look for



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.

Examples:

Matching multiples of “xyz”

verbal = Verbal.new { multiple 'xyz' }
verbal.match('this is xyzxyz')[0] # => 'xyzxyz'

Matching multiples of /[xyz]/

verbal = Verbal.new { multiple /[xyz]/ }
verbal.match('abcxxyz')[0] # => 'xxyz'

Parameters:

  • value (String|Regexp)

    string to match



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.

Examples:

Tests if the string begins with ‘http://’ or ‘ftp://’

link = 'ftp://ftp.google.com/';
verbal = Verbal.new do
  find 'http'
  maybe 's'
  find '://'
  otherwise
  find 'ftp://'
end
link =~ verbal # => 0


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.

Examples:

Find any character in the alphabet.

verbal = Verbal.new do
  range 'a', 'z', 'A', 'Z'
end

Parameters:

  • args (Array)

    alternate starting and ending characters.



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_lineObject

Marks the expression to start at the beginning of a line.

Examples:

Add a hyphen to the beginning of each line.

lines = "first\nsecond\nthird"
verbal = Verbal.new do
  start_of_line
end
lines.gsub(verbal, '- ') # => "- first\n- second\n- third"


41
42
43
# File 'lib/verbal.rb', line 41

def start_of_line
  @prefixes = '^'
end

#start_of_stringObject

Marks the expression to start at the beginning of the string.

Examples:

Matching the entire string

verbal = Verbal.new do
  start_of_string
  find 'dinosaur'
  end_of_string
end
verbal.match('dinosaur')  # matches
verbal.match('a dinosaur') # does not match


65
66
67
# File 'lib/verbal.rb', line 65

def start_of_string
  @prefixes = '\A' + @prefixes
end

#tabObject

Matches the tab character.

Examples:

Find the tab character.

"\n\t" =~ Verbal.new { tab | # => 1


127
128
129
# File 'lib/verbal.rb', line 127

def tab
  append '\t'
end

#wordObject

Matches a word, which is a continuous chunk of any alphanumeric character.

Examples:

Split a sentence into words.

s = "this is a sentence"
s.scan Verbal.new { word } # => ["this", "is", "a", "sentence"]


135
136
137
# File 'lib/verbal.rb', line 135

def word
  append '\w+'
end