^([a-zA-Z0-9_-]+)$
matches:
BAP-78810
BAP-148080
But does not match:
B8241066 C
Q2111999 A
Q2111999 B
How can I modify regex pattern to match any space and/or special character?
For the example data, you can write the pattern as:
^[a-zA-Z0-9_-]+(?: [A-Z])?$
^
Start of string[a-zA-Z0-9_-]+
Match 1+ chars listed in the character class(?: [A-Z])?
Optionally match a space and a char A-Z$
End of stringOr a more exact match:
^[A-Z]+-?\d+(?: [A-Z])?$
^
Start of string[A-Z]+-?
Match 1+ chars A-Z and optional -
\d+(?: [A-Z])?
Matchh 1+ digits and optional space and char A-Z$
End of string