I'm taking the Nand-2-Tetris course. We are asked to write and assembler. A C-command is in the type of dest=comp;jump
where each part is optional.
I was trying to write a regex to make everything easier - I want to be able to compile the expression on a given line, and just by the group number, know which part of the expression I'm using. For example, for the expression: A=M+1;JMP
I want to get group(1) = A
, group(2) = M
and group(3) = JMP
.
My problem is that each part is optional, so I don't know exactly how to write this regex. So far I come up with:
(A?M?D?)\s=([^;\s]*)\s?(?=;[\s]*([a-zA-Z]{1,4})|$)
This works for most cases, but it doesn't work as I expect it. For example, lack of comp won't work (D;JGT
). I have tried positive lookahead but it didn't work.