Reputation: 12077
I create a regular expression on the fly based on the results of a variable. I've successfully tested most of the expression, but I'm struggling with the part that can vary in length.
How can I amend the regular expression (a_)?c(davison)\.nsf?
so that any of the below strings would be classed as a match?
Essentially, to be considered a match, strings must meet the following criteria -
Any hints and tips would be appreciated.
Upvotes: 2
Views: 100
Reputation: 627410
To make each part of davison
optional, use nested optional groups ((?:...)?
), and to set the length restriction, you may use a negative lookahead anchored at the start to fail the match if 13 characters are found (thus, (?!.{13})
will allow strings of 12 and fewer characters in size):
^(?!.{13})(a_)?c(d(?:a(?:v(?:i(?:s(?:on?)?)?)?)?)?)\.nsf$
See the regex demo
Upvotes: 5