When you are building an alternation (e.g. (A|B|AB|O)
), you should be careful with the ordering of the elements. Many regex engines will stop at the first alternate that matches (rather than the longest). If it weren't for the [-+]
forcing a backtrack, (A|B|AB|O)[-+]
would not work for "AB+"
. It is probably better to say (AB|A|B|O)[-+]
(but you should check the docs for your regex engine).
Also, if you do not intend to capture the antigen for latter use, you should you use the non-capturing grouping parentheses: (?:AB|A|B|O)[-+]
.
Furthermore, if you want to ensure that the only thing in the string is a blood type then you need anchors to prevent it from matching only part of the string: ^(?:AB|A|B|O)[-+]$
. A quick note on anchors, Depending on your regex engine, ^
may match the beginning of a line rather than the beginning of the string if you pass it a multiline-match option. Similarly, $
may match the end of a line rather than the end of a string. For this reason there are three other anchors in common (but not %100) usage: \A
, \Z
, and \z
. If your regex engine supports them, \A
always matches the start of the string, \Z
matches the end of the string or a newline just before the end of the string, and \z
matches only the send of the string.