Skip to content

Instantly share code, notes, and snippets.

@emkay
Created October 16, 2011 20:47
Show Gist options
  • Save emkay/1291402 to your computer and use it in GitHub Desktop.
Save emkay/1291402 to your computer and use it in GitHub Desktop.
Ruby regex matcher
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