Why are people using regexp for email and other complex validation?

后端 未结 12 1348
感情败类
感情败类 2020-12-16 16:01

There are a number of email regexp questions popping up here, and I\'m honestly baffled why people are using these insanely obtuse matching expressions rather than a very si

12条回答
  •  臣服心动
    2020-12-16 16:47

    People do it because in most languages it is way easier to write regexp than to write and use a parser in your code (or so it seems, at least).

    If you decide to eschew regexes, you will have to either write parsers by hand, or you resort to external tools (like yacc) for lexer/parser generation. This is way more complex than single-line regex match.

    One need to have a library that makes it easy to write parsers directly in the language X (where 'X' is C, C++, C#, Java) to be able to build custom parsers with the same ease as regular expression matchers.

    Such libraries originated in the functional land (Haskell and ML), but nowadays "parser combinators libraries" exist for Java, C++, C#, Scala and other mainstream languages.

提交回复
热议问题