I made a regex Debuggex Demo which allows:
- NP123
- FS-123
- FS-123-456
- or void
works well on Debuggex, but and I don’t understand why PHP accept strings like “TEST” which is not part of the accepted templates
Check the fiddler: https://www.phpliveregex.com/p/teA
Advertisement
Answer
You may use
^(?:FS-[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?|NP[0-9a-zA-Z]+)?$
See the regex demo
Details
^
– start of string(?:
– start of an outer non-capturing group for the anchors to be applied to all the alternatives insideFS-[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?
–FS-
and 1+ alphanumeric chars and then an optional sequence of-
and 1+ alphanumeric chars|
– orNP[0-9a-zA-Z]+
–NP
and 1+ alphanumeric chars
)?
– end of the group$
– end of string.