Created
December 12, 2018 02:36
-
-
Save devssun/d1c0d20daa300c08cc921824a912eb72 to your computer and use it in GitHub Desktop.
Swift String to String Array 정규식 일치 추출 함수
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
func matches(for regex: String, in text: String) -> [String] { | |
// [Ios] 신속한 정규식 일치 추출 : https://code.i-harness.com/ko/q/1a96cca | |
do { | |
let regex = try NSRegularExpression(pattern: regex, options: []) | |
let nsString = NSString(string: text) | |
let results = regex.matches(in: text, options: [], range: NSRange.init(location: 0, length: nsString.length)) | |
return results.map {nsString.substring(with: $0.range)} | |
}catch let error { | |
print("invalid regex: \(error.localizedDescription)") | |
return [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment