Regex vs Tryparse what is the best in performance

后端 未结 4 1282
日久生厌
日久生厌 2021-01-17 16:25

In my ASP.net project I need to validate some basic data types for user inputs. The data types are like numeric, decimal, datetime etc.

What is the best approach t

4条回答
  •  [愿得一人]
    2021-01-17 16:36

    Don't try to make regexes do everything.

    Sometimes a simple regex will get you 90% of the way and to make it do everything you need the complexity grows ten times or more.

    Then I often find that the simplest solution is to use the regex to check the form and then rely on good old code for the value checking.

    Take a date for example, use a regex to check for a match on a date format and then use capturing groups to check the values of the individual values.

提交回复
热议问题