title: regex matchesInString in Swift 2
body: <p>The <code>toCamel()</code> function in this String extension is supposed to remove any <code>_</code> followed by a character in the middle of a String, and replace the two with the character to uppercase. </p>
  
  <pre><code>public extension String {
      public func rangeFromNSRange(aRange: NSRange) -&gt; Range&lt;String.Index&gt; {
          let s = advance(self.startIndex, aRange.location)
          let e = advance(self.startIndex, aRange.location + aRange.length)
          return s..&lt;e
      }
      public var ns : NSString {return self as NSString}
      public subscript (aRange: NSRange) -&gt; String? {
          get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
      }
  
      public var cdr: String {return isEmpty ? "" : String(characters.dropFirst())}
  
      public func toCamel() throws -&gt; String {
          var s = self
          let regex = try NSRegularExpression(pattern: "_.", options: [])
          let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
          for match in matches {
              print("match = \(s[match.range]!)")
              let matchRange = s.rangeFromNSRange(match.range)
              let uc = s[match.range]!.uppercaseString.cdr
              s.replaceRange(matchRange, with: uc)
          }
          if s.hasPrefix("_") {s = s.cdr}
          return s
      }
  }
  </code></pre>
  
  <p>The thing is that <code>matchesInString</code> doesn't return the matches where the <code>_</code>is already part of the previous match. And thus, it gives the following results :</p>
  
  <pre><code>try "hello_world".toCamel()    //"helloWorld"
  
  try "hello__world".toCamel()   //"hello_world"
  
  try "hello___world".toCamel()  //"hello_World"
  </code></pre>
  
  <p>How to go around this?</p>