Hey, nice job.
Actually, you can fit it in less than 40 lines just reordering some of the clauses and using the fact that:
case matching(Something) of true -> true; false -> matching(SomethingElse) end
is equivalent to:
matching(Something) orelse matching(SomethingElse)
since matching/2 returns a boolean.
matching/2
Here's my (I believe equivalent) version: (I used search and match names since they are similar to how python re module works, where search looks anywhere in the string and match has to match from the beginning of the string)
search
match
re
-module(regex). -export([search/2, match/2]). search(String, [$^ | Regex]) -> match(String, Regex); search([Char | String], Regex = [_ | _]) -> search(String, Regex) orelse match([Char | String], Regex); search(_String, _Regex) -> false. match(_String, []) -> true; match([], [$$]) -> true; match(String, [Char | [$? | Regex]]) -> match(String, Regex) orelse match(String, [Char | Regex]); match([_Char | String], [$. | [$* | Regex]]) -> match(String, Regex) orelse match(String, [$. | [$* | Regex]]); match([Char | String], [Char | [$* | Regex]]) -> match(String, Regex) orelse match(String, [Char | [$* | Regex]]); match(String, [_Char | [$* | Regex]]) -> match(String, Regex); match([_Char | String], [$. | Regex]) -> match(String, Regex); match([Char | String], [Char | Regex]) -> match(String, Regex); match(String, [$^ | Regex]) -> match(String, Regex); match(_String, _Regex) -> false.
Haven't tested a lot of cases, but seems to be working. :)
Nice catch!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey, nice job.
Actually, you can fit it in less than 40 lines just reordering some of the clauses and using the fact that:
is equivalent to:
since
matching/2
returns a boolean.Here's my (I believe equivalent) version:
(I used
search
andmatch
names since they are similar to how pythonre
module works, wheresearch
looks anywhere in the string andmatch
has to match from the beginning of the string)Haven't tested a lot of cases, but seems to be working. :)
Nice catch!