问题
Code Example
14> case re:run("5162754", "/^\d+$/") of {match, _} -> ok end.
** exception error: no case clause matching nomatch
15> case re:run(<<"5162754">>, "/^\d+$/") of {match, _} -> ok end.
** exception error: no case clause matching nomatch
Why doesn't it match?
回答1:
Two things:
The regular expression you pass to
re:runshouldn't be surrounded by/. In other languages, you write a regexp inside/signs, but in Erlang, regexps are always written as strings, and thus no/signs are necessary.In an Erlang string,
\drepresents the "delete" character (code 127). What you actually want in your regexp is a backslash followed by the letterd. To achieve that, you need to escape the backslash with another backslash:> re:run("5162754", "^\\d+$"). {match,[{0,7}]}
回答2:
Try to use [0-9] it will also works and no problems with backslashes
re:run("5162754", "^[0-9]+$").
来源:https://stackoverflow.com/questions/28475485/why-doesnt-work-regex