Why doesnt work regex

本秂侑毒 提交于 2019-12-25 01:46:44

问题


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:

  1. The regular expression you pass to re:run shouldn'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.

  2. In an Erlang string, \d represents the "delete" character (code 127). What you actually want in your regexp is a backslash followed by the letter d. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!