p=re.compile(A|B)
You are not doing the string concatenation correctly. What you are doing is applying the "bitwise or" (the pipe) operator to strings, which, of course, fails:
>>> 'aaa' | 'bbb'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'str' and 'str'
Instead, you can use str.join()
:
p = re.compile(r"|".join([A, B]))
Demo:
>>> A = 'aaa'
>>> B = 'bbb'
>>> r"|".join([A, B])
'aaa|bbb'
And, make sure you trust the source of A
and B
(beware of Regex injection attacks), or/and properly escape them.