My regex code [A-Z]{1,}\d{3,}\w?
works fine returning strings like CX3623, M3326, Y2362 but I also want to be able to return strings which are in the following format:
YH321-2
V2021/V2022
1.2A-2351
YGH256-4268
What should I add to the regex?
For the first part, you could match the different formats using an alternation.
You could make the second part optional using an optional non capturing group (?:...)?
and match either /
or -
optionally followed by chars A-Z and 1+ digits.
\b(?:[A-Z]+ )?(?:[A-Z]*\d{3,}|\d+(?:\.\d+)?[A-Z]+)(?:[\/-][A-Z]*\d+)?\b
Explanation
\b
Word boundary(?:[A-Z]+ )?
Optionally match 1+ chars A-Z followed by a space(?:
Non capture group
[A-Z]*\d{3,}
Match 0+ times A-Z and 3 or more digits|
Or\d+(?:\.\d+)?[A-Z]+
Match 1+ digits with an optional decimal part and 1+ times A-Z)
Close group(?:
Non capture group
[\/-][A-Z]*\d+
Match either /
or -
, 0+ times A-Z and 1+ digits)?
Close group and make optional\b
Word boundary