Skip to content

Instantly share code, notes, and snippets.

@devssun
Created December 12, 2018 02:36
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 devssun/d1c0d20daa300c08cc921824a912eb72 to your computer and use it in GitHub Desktop.
Save devssun/d1c0d20daa300c08cc921824a912eb72 to your computer and use it in GitHub Desktop.
Swift String to String Array 정규식 일치 추출 함수
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