title: Swift replace substring regex
body: <p>I am attempting to use regular expression to replace all occurrences of UK car registrations within a string.</p>
  
  <p>The following swift code works perfectly for a when the string matches the regex exactly as below.</p>
  
  <pre><code>var myString = "DD11 AAA"
  var stringlength = countElements(myString) 
  var ierror: NSError?
  var regex:NSRegularExpression = NSRegularExpression(pattern: "^([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}$", options: NSRegularExpressionOptions.CaseInsensitive, error: &amp;ierror)!
  var modString = regex.stringByReplacingMatchesInString(myString, options: nil, range: NSMakeRange(0, stringlength), withTemplate: "XX")
  print(modString)
  </code></pre>
  
  <p>The result is <code>XX</code></p>
  
  <p>However, the following does not work and the string is not modifed</p>
  
  <pre><code>var myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
  var stringlength = countElements(myString) 
  var ierror: NSError?
  var regex:NSRegularExpression = NSRegularExpression(pattern: "^([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}$", options: NSRegularExpressionOptions.CaseInsensitive, error: &amp;ierror)!
  var modString = regex.stringByReplacingMatchesInString(myString, options: nil, range: NSMakeRange(0, stringlength), withTemplate: "XX")
  print(modString)
  </code></pre>
  
  <p>The result is <code>my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB</code></p>
  
  <p>Can anyone give me any pointers?</p>