Last active
December 17, 2015 03:49
-
-
Save NathanielWroblewski/5545945 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
def scan(regex, text) | |
return match_found?(regex[1..-1], text) if regex[0] == '^' | |
until text[0].nil? | |
match_found?(regex, text) ? (return true) : text = text[1..-1] | |
end | |
false | |
end | |
def match_found?(regex, text) | |
if regex[0] == nil then return true | |
elsif regex[1] == '*' then return splat_match?(regex[0],regex[2..-1],text) | |
elsif regex[0] == '$' && regex[1] == nil then return text[0].nil? | |
elsif text[0] != nil && (regex[0] == '.' || regex[0] == text[0]) | |
return match_found?(regex[1..-1], text[1..-1]) | |
end | |
false | |
end | |
def splat_match?(current_regex, next_regex, text) | |
until text[0].nil? && (text[0] == current_regex || current_regex == '.') | |
match_found?(next_regex, text) ? (return true) : text = text[1..-1] | |
end | |
false | |
end | |
p scan('bc','abc') | |
p scan('disco','stu') == false | |
p scan('a*','abc') | |
p scan('^a','abc') | |
p scan('aster .*aster','master blaster') | |
p scan('a$','bca') | |
p scan('a$','abc') == false | |
p scan('.*','abc') | |
p scan('.*g','nathanielwroblewski@gmail.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment