I working on a regular expression which returns 1 only in case given value is A or B.
I used
select 'A' REGEXP '^A|B$'
The '|' symbol is behaving like "or", but the result is not as expected with AB:
select 'AB' REGEXP '^A|B$' = 1
while I expect no match here.
Your current solution matches
^A-Aat the start of the string|- orB$-Bat the end of the string.The
REGEXPoperator can return partial matches (unlikeLIKEoperator that requires the wildcard pattern to match the whole string), and thus can matchABCandCAB. See the regex demo.You may use
The
A|Bis inside a grouping construct here and^and$both modify theAandBbranch. See this regex demo.If these
AandBare single chars, use a[AB]bracket expression:where
[AB]matchesAorB, and the^/$anchors modify each letter inside the brackets.See this regex demo.