Question

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.

  1. Would have to include 32 bit integers, and things like 8.1, and all kinds of other edge cases
  2. 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.
  3. While somewhat doable, would require a recursive regex
  4. Could include one of five versions, potentially XML schemas, SGML dtds and all kinds of absurd stuff.
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
scroll top