Skip to content

Instantly share code, notes, and snippets.

@mathewsanders
Created April 25, 2016 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewsanders/f746a77ef7ad0b2217908631bee29b11 to your computer and use it in GitHub Desktop.
Save mathewsanders/f746a77ef7ad0b2217908631bee29b11 to your computer and use it in GitHub Desktop.
Using a regular expression to check for a possible rule structure
let pattern = "(greater|more|)(.+)(than|)(.+)"
let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
let text = "more blue triangles than red circles"
let matches = regex.matchesInString(text, options: [], range: NSRange(location: 0, length: text.characters.count))
if let match = matches.first {
let seperator1 = (text as NSString).substringWithRange(match.rangeAtIndex(1))
let fragment1 = (text as NSString).substringWithRange(match.rangeAtIndex(2))
let seperator2 = (text as NSString).substringWithRange(match.rangeAtIndex(3))
let fragment2 = (text as NSString).substringWithRange(match.rangeAtIndex(4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment