Created
April 25, 2016 00:35
-
-
Save mathewsanders/f746a77ef7ad0b2217908631bee29b11 to your computer and use it in GitHub Desktop.
Using a regular expression to check for a possible rule structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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