Last active
May 1, 2017 23:53
-
-
Save danielt1263/363a921febca46295e19ab8a875b412d to your computer and use it in GitHub Desktop.
Make regexs simpler in Swift.
This file contains hidden or 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
// | |
// Regex.swift | |
// | |
// Created by Daniel Tartaglia on 2/6/16. | |
// Copyright © 2016 Daniel Tartaglia. MIT License. | |
// | |
import Foundation | |
struct Regex { | |
init?(_ pattern: String, options: NSRegularExpression.Options = .caseInsensitive) { | |
do { | |
regularExpression = try NSRegularExpression(pattern: pattern, options: options) | |
} | |
catch { | |
return nil | |
} | |
} | |
func match(_ string: String, options: NSRegularExpression.MatchingOptions = .reportCompletion) -> Bool { | |
return regularExpression.numberOfMatches(in: string, options: options, range: NSMakeRange(0, string.utf16.count)) != 0 | |
} | |
let regularExpression: NSRegularExpression | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment