Created
October 16, 2011 20:47
-
-
Save emkay/1291402 to your computer and use it in GitHub Desktop.
Ruby regex matcher
This file contains hidden or 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 match(regexp, text) | |
if (regexp.nil? or regexp.empty?) or (regexp[0..0] == '$' and text.empty?) then return true end | |
if regexp[0..0] == '^' then return match(regexp[1..regexp.size-1], text) end | |
if regexp[0..0] == '*' then return match(regexp[2..regexp.size-1], text[star(regexp[1..1], text)..text.size-1]) end | |
if text[0..0] == regexp[0..0] or regexp[0..0] == '.' then return match(regexp[1..regexp.size-1], text[1..text.size-1]) end | |
false | |
end | |
def star(char, text, count=0) | |
if char == text[0..0] | |
count += 1 | |
return matchstar(char, text[1..text.size-1], count) | |
end | |
count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment