Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule DotExp do
@doc """
RegExp like syntax validator, "." is any symbol,
"*" repeats last symbol 0..n times.
"""
def match?(expr, str), do: match(expr, str)
defp match("", ""), do: true
defp match(".*", _), do: true
defp match(".*" <> expr, str),
do: Enum.any?(0..String.length(str),
fn(x)-> match(expr, String.slice(str, x..-1)) end)
defp match(<<c::utf8, "*", expr::binary>>, str),
do: match(expr, String.lstrip(str, c))
defp match("." <> expr, << _::utf8, str::binary >>),
do: match(expr, str)
defp match(<< _n::utf8, expr::binary >>, << _n::utf8, str::binary >>),
do: match(expr, str)
defp match("", _), do: false
defp match(_, _), do: false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment