0

This occurs so frequently in validation RegExps I was wondering whether there was a more efficient form that I'm missing.

Consider the following RegExp: ^[\w\s-&’'!À-Üà-øoù-ÿŒœ]+(\|[\w\s-&’'!À-Üà-øoù-ÿŒœ]+)*$

All it's doing is validating all of the following:

  • l'écureuil
  • losange|l'hibou|Œdeppe

What is frustrating me is that I must repeat the entire capture class twice, just so I can handle the cases of a single entry (with no leading delimiter), and then cases where there are multiple groups separated by the (required) delimiter

\1 doesn't work here of course because I'm not matching the same string.

(this question is not about how I can capture French words using a more succinct class definition - that's just an arbitrary example)

Tom Auger
  • 19,421
  • 22
  • 81
  • 104

0 Answers0