title: How to stop regex expression at certain string and capture the sub string?
body: <p>I want to capture the string in between the two matches of the regex expression.</p>
  
  <p>I have this string</p>
  
  <pre><code>var testingString="NO Lettuce nNO Onions nNO Tomato nNO Ketchup NO  Grilled Onions NO baked Sub Zapps"
  </code></pre>
  
  <p>The regex expression I am using now is:</p>
  
  <pre><code>let matches = self.regexMatches(for: "([^\\snNO]+|[NO]\\S+([a-zA-Z0-9_\\w+])+\\S)", in: testingString)
  </code></pre>
  
  <p>I am getting output as:</p>
  
  <pre>
  Lettuce
  Onions
  Tomato
  Ketchup
  Grilled
  Onions
  baked
  sub
  zapps
  </pre>
  
  <p>But I want the output to be as:</p>
  
  <pre>
  Lettuce
  Onions
  Tomato
  Ketchup
  Grilled Onions
  baked sub zapps
  </pre>
  
  <p>This is the regexMatches function I am using:</p>
  
  <pre><code>func regexMatches(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))
          var tempResults = results.map { result in
              (1..&lt;result.numberOfRanges - 1).map { result.rangeAt($0).location != NSNotFound
                  ? nsString.substring(with: result.rangeAt($0))
                  : ""
              }
          }
          return tempResults.flatMap{$0}
      } catch let error {
          print("invalid regex: \(error.localizedDescription)")
          return []
      }
  }
  </code></pre>