title: Regular expression for UK postal codes
body: <p>I'm making an application that asks the user to enter a postcode and outputs the postcode if it is valid.</p>
  
  <p>I found the following pattern, which works correctly:</p>
  
  <pre><code>String pattern = "^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) [0-9][ABD-HJLNP-UW-Z]{2})"; 
  </code></pre>
  
  <p>I don't know much about regex and it would be great if someone could talk me through this statement. I mainly don't understand the <code>?</code> and use of <code>()</code>.</p>