title: trying to parse a Localizable.string file for a small project in swift on MacOS
body: <p>I'm trying to parse a Localizable.string file for a small project in swift on MacOS.
  I just want to retrieve all the keys and values inside a file to sort them into a dictionary.</p>
  
  <p>To do so I used regex with the <code>NSRegularExpression</code> cocoa class.</p>
  
  <p>Here is what those file look like :</p>
  
  <pre><code>"key 1" = "Value 1";
  "key 2" = "Value 2";
  "key 3" = "Value 3";
  </code></pre>
  
  <p>Here is my code that is supposed to get the keys and values from the file loaded into a <code>String</code> :</p>
  
  <pre><code>static func getDictionaryFormText(text: String) -&gt; [String: String] {
      var dict: [String : String] = [:]
      let exp = "\"(.*)\"[ ]*=[ ]*\"(.*)\";"
  
      for line in text.components(separatedBy: "\n") {
          let match = self.matches(for: exp, in: line)
          // Following line can be uncommented when working
          //dict[match[0]] = match[1]
          print("(\(match.count)) matches = \(match)")
      }
      return dict
  }
  
  static func matches(for regex: String, in text: String) -&gt; [String] {
      do {
          let regex = try NSRegularExpression(pattern: regex)
          let nsString = text as NSString
          let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
          return results.map { nsString.substring(with: $0.range) }
      } catch let error as NSError {
          print("invalid regex: \(error.localizedDescription)")
          return []
      }
  }
  </code></pre>
  
  <p>When running this code with the provided Localizable example here is the output :</p>
  
  <pre><code>(1) matches = ["\"key 1\" = \"Value 1\";"]
  (1) matches = ["\"key 2\" = \"Value 2\";"]
  (1) matches = ["\"key 3\" = \"Value 3\";"]
  </code></pre>
  
  <p>It sounds like the match doesn't stop after the first <code>"</code> occurence. When i try the same expression <code>\"(.*)\"[ ]*=[ ]*\"(.*)\";</code> on regex101.com the output is correct though. What am i doing wrong ?</p>