When not to use Regex in C# (or Java, C++, etc.)

后端 未结 7 1210
囚心锁ツ
囚心锁ツ 2020-11-28 13:12

It is clear that there are lots of problems that look like a simple regex expression will solve, but which prove to be very hard to solve with regex.

<
7条回答
  •  渐次进展
    2020-11-28 14:14

    There are two aspects to consider:

    • Capability: is the language you are trying to recognize a Type-3 language (a regular one)? if so, then you might use regex, if not, you need a more powerful tool.

    • Maintainability: If it takes more time write, test and understand a regular expression than its programmatic counterpart, then it's not appropriate. How to check this is complicated, I'd recommend peer review with your fellows (if they say "what the ..." when they see it, then it's too complicated) or just leave it undocumented for a few days and then take a look by yourself and measure how long does it take to understand it.

提交回复
热议问题