What is the best use case of a regular expression (TripleByte question)?
https://softwareengineering.stackexchange.com/questions/402197
-
05-03-2021 - |
質問
I have lots of experience with regexes. I just want to make sense of this. Am I missing something here. Which one of these would you rather validate with a regex? Question reads,
Which of the following is the best use case for a regular express?
Select the correct answer:
- Validate an IP address
- Validate a SQL query
- Validate JSON text
- Validate HTML
I'm not sure it would be easy to validate any of these with a regex.
- Would have to include 32 bit integers, and things like 8.1, and all kinds of other edge cases
- Is incredibly complex and even if were just talking about a spec you'd be talking about thousands of SLOC inside of the regex for the grammer.
- While somewhat doable, would require a recursive regex
- Could include one of five versions, potentially XML schemas, SGML dtds and all kinds of absurd stuff.
解決
I agree that I wouldn't use a regular expression for any of them, but if I had to choose, I would go for the IP address, because the other ones all have a nested structure, and are therefore not well-suited for regular expressions, which is what I think they are trying to get at.
所属していません softwareengineering.stackexchange