-
-
Save DouweM/6269004 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a program trying to implement Verbal Expressions | |
# See this for more info - http://verbalexpressions.github.io/ | |
def VerEx | |
VerExClass.new | |
end | |
class VerExClass | |
attr_accessor :regex | |
def intitialize(regex = "") | |
self.regex = regex | |
end | |
def final_regexp | |
Regexp.new(regex) | |
end | |
def add(value) | |
regex << value | |
self | |
end | |
alias_method :<<, :add | |
def start_of_line | |
add("^") | |
end | |
def end_of_line | |
add("$") | |
end | |
def find(value) | |
add("(" + Regexp.escape(value) + ")") | |
end | |
def then(value) | |
find(Regexp.escape(value)) | |
end | |
def maybe(value) | |
add("(" + Regexp.escape(value) + ")?") | |
end | |
def anything | |
add("(?:.*)") | |
end | |
def anything_but(value) | |
add("([^" + Regexp.escape(value) + "]*)") | |
end | |
def something | |
add("(?:.+)") | |
end | |
def something_but(value) | |
add("(?:[^" + Regexp.escape(value) + "]+)") | |
end | |
def line_break | |
add("(?:(?:\n)|(?:\r\n))") | |
end | |
def tab | |
add("\t") | |
end | |
def match(string) | |
final_regexp.match(string) | |
end | |
end | |
# Tests - | |
if __FILE__ == $0 | |
verex = VerEx. | |
start_of_line. | |
find('http'). | |
maybe('s'). | |
find('://'). | |
maybe('www.'). | |
anything_but(' '). | |
end_of_line | |
if verex.match("https://www.google.com") | |
puts "Matched" | |
else | |
puts "Not matched" | |
end | |
puts verex.final_regexp | |
puts "^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$" | |
if verex.final_regexp.match("https://google.com") | |
puts "True" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment