A Regular Expression Conundrum

Here’s a little quiz to test your regex-fu. Suppose you want to match any of the strings

  • ab,cd
  • ab-cd
  • ab.cd

Someone proposes the regex ab[,-.]cd but you object that that’s incorrect because the - will be treated as specifying the range of characters between , and .. Oddly, the regex does match all three strings. Why is that?

See Paul Boyd’s post for the answer.

UPDATE [2022-07-05 Tue 14:04]:

I should have said that the regex matches all three strings and only those strings.

This entry was posted in General and tagged . Bookmark the permalink.