title: RegEx help for chess moves (SAN)
body: <p>I'm writing a program that should be able to read and parse chess moves (<a href="https://en.wikipedia.org/wiki/Algebraic_notation_(chess)" rel="noreferrer">SAN</a>). </p>
  
  <p>Here's an example of possible accepted moves:</p>
  
  <pre><code>e4
  Nf3
  Nbd2
  Nb1c3
  R1a3
  d8=Q
  exd5
  Nbxd2
  ...
  </code></pre>
  
  <p>I first wrote the <a href="https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton" rel="noreferrer">NFA</a>, then converted it to grammar and then I converted it to a regular expression.</p>
  
  <p>With my conventions, this is how it looks</p>
  
  <pre><code>pln + plxln + plnxln + plnln + plln + pxln + lxln=(B+R+Q+N) + lxln + lnxln=(B+R+Q+N) + lnxln + lnln=(B+R+Q+N) + lnln + ln=(B+R+Q+N) + ln + pnxln + pnln
  </code></pre>
  
  <p>where:</p>
  
  <p><code>p</code> is a character of set <code>{B,R,Q,N,K}</code> (or think it as <code>(B+R+Q+N+K)</code> = <code>[BRQNK]</code></p>
  
  <p><code>l</code> is a character among <code>[a-h]</code> interval (case sensitive)</p>
  
  <p><code>n</code> is a number among <code>[1-8]</code> interval</p>
  
  <p><code>+</code> represents Union operation... if I got it right, <code>(B+R+Q+N)</code> is <code>[BRQN]</code> in regex's programming languages.</p>
  
  <p><code>=</code> is just a normal character... in chess moves it's used in promotion (ex. e8=Q)</p>
  
  <p><code>x</code> is a normal character too... used when by moving your piece in that location you're taking an opponent's one.</p>
  
  <p><code>(</code>/<code>)</code>: Like in math</p>
  
  <p>I tried to parse first part <code>pln</code> as: <code>[BRQN][a-h][1-8]</code> in an online java regex tester and worked for a move like <code>Nf3</code>. I didn't get well how to do the union thing for composite expression (like <code>pln+plxln</code>)... also how can I label to parts of regex so that when it's detected, I get all the infos? I tried to read docs about it but didn't figure out.</p>
  
  <p>Any advice?</p>