Class: Regexify
- Inherits:
-
Object
- Object
- Regexify
- Defined in:
- lib/regexify.rb
Overview
Where the regex magic happens
Defined Under Namespace
Classes: Error
Constant Summary collapse
- PATTERNS =
a small list of popular regex tokens that are available with regexify
{ number: '0-9', uppercase: 'A-Z', lowercase: 'a-z', letter: 'a-zA-Z', alphanumeric: 'a-zA-Z0-9', anything: '.', whitespace: '\\s', tab: '\\t', space: ' ' }
- ESCAPED_CHARS =
chars that needs to be escaped in a regex
%w(* . ? ^ + $ | ( ) [ ] { } \\)
Instance Method Summary collapse
-
#begin_with(*args, exactly: nil, range: nil) ⇒ Regexify
Defines the beginning of the regex and adds ‘^`.
-
#end_with(*args, exactly: nil, range: nil) ⇒ Regexify
Defines the ending of the regex and adds ‘$`.
-
#initialize ⇒ Regexify
constructor
default constructor.
-
#not(*args, exactly: nil, range: nil) ⇒ Regexify
Adds a new part to the regex that is negated using ‘^`.
-
#regex ⇒ Object
Converts Regexify object to Regexp.
-
#then(*args, exactly: nil, range: nil) ⇒ Regexify
Adds a new part to the regex.
Constructor Details
#initialize ⇒ Regexify
default constructor
24 25 26 27 |
# File 'lib/regexify.rb', line 24 def initialize @str = "" @complete = false end |
Instance Method Details
#begin_with(*args, exactly: nil, range: nil) ⇒ Regexify
Defines the beginning of the regex and adds ‘^`
34 35 36 37 38 |
# File 'lib/regexify.rb', line 34 def begin_with(*args, exactly: nil, range: nil) raise Regexify::Error.new('#begin_with? called multiple times') unless @str.empty? @str += "^#{parse(args, exactly: exactly, range: range)}" self end |
#end_with(*args, exactly: nil, range: nil) ⇒ Regexify
Defines the ending of the regex and adds ‘$`
45 46 47 48 49 50 |
# File 'lib/regexify.rb', line 45 def end_with(*args, exactly: nil, range: nil) raise Regexify::Error.new('#end_with? called multiple times') if @complete @str += "#{parse(args, exactly: exactly, range: range)}$" @complete = true self end |
#not(*args, exactly: nil, range: nil) ⇒ Regexify
Adds a new part to the regex that is negated using ‘^`
67 68 69 70 |
# File 'lib/regexify.rb', line 67 def not(*args, exactly: nil, range: nil) @str += parse(args, exactly: exactly, range: range).insert(1, "^") self end |
#regex ⇒ Object
Converts Regexify object to Regexp
73 74 75 |
# File 'lib/regexify.rb', line 73 def regex Regexp.new(@str) end |
#then(*args, exactly: nil, range: nil) ⇒ Regexify
Adds a new part to the regex
57 58 59 60 |
# File 'lib/regexify.rb', line 57 def then(*args, exactly: nil, range: nil) @str += parse(args, exactly: exactly, range: range) self end |