My question is very similar to this one, except that I want to make each alternation optional, and non-repetitive.
Example: The regex needs to match the following strings (this is oversimplified, A and B can be complex):
XAB
XBA
XA
XB
X
It can NOT match XAA, XBB, XABA, XABB, XBAA
Here is what I have so far:
/(X)(?:(A)|(B)){0,2}$/
This allows for repetition (such as XAA), and seems to cause a problem with XB in PHP with an empty array element.
EDIT: Forgot to mention that I need to get the actual values of X, A, and B (if available). It's not just a match of the full string.
Use negative lookahead assertions:
Explanation: