You almost had it with your original regex.
It just needs a couple of tweaks:
^(\w+)(=(\w+))?$
^ = start of string 
(\w+) = 1st capture group matching any word like character (including numbers) as many times as possible. 
(=...)? = everything inside this 2nd capture group (starting with "=") is optional 
- 2nd 
(\w+) = 3rd capture group matching the same stuff as the first one 
$ = end of string 
update
My answer does not actually answer the original question because the "X" string does not land "X" in the second answer group.
I considered deleting the answer, but I'm going to keep it up for the sake of other visitors on the site who are looking for a simpler answer that does not require "X" to specifically be in the second capture group.
Also, maybe the original asker would rather have a simpler regex and modify their code to work with the regex instead of making a regex to work with the code.