Last active
December 16, 2015 16:53
-
-
Save coa00/c57e30af432c4fdfb43b to your computer and use it in GitHub Desktop.
Swift2.0で正規表現を簡単に扱う。 ref: http://qiita.com/coa00@github/items/ae9c38dc92f3626dcd19
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
| let pattern = "http://([a-zA-Z0-9]|.)+" | |
| let str:String = "銘柄コード:1557,銘柄名:SPDR S&P500 ETF TRUST板価格:25270.0,板数量:10000にいびつな板(寄与率:81.20178%)を検出しました。http://oreore.com/servlets/Action?SRC=1234" | |
| Regexp(pattern).isMatch(str) //マッチした結果 ここではtrue | |
| let ret:[String] = Regexp(pattern).matches(str)! //http以下を取得 |
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
| import Foundation | |
| class Regexp { | |
| let internalRegexp: NSRegularExpression | |
| let pattern: String | |
| init(_ pattern: String) { | |
| self.pattern = pattern | |
| self.internalRegexp = try! NSRegularExpression( pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive) | |
| } | |
| func isMatch(input: String) -> Bool { | |
| let matches = self.internalRegexp.matchesInString( input, options: [], range:NSMakeRange(0, input.characters.count) ) | |
| return matches.count > 0 | |
| } | |
| func matches(input: String) -> [String]? { | |
| if self.isMatch(input) { | |
| let matches = self.internalRegexp.matchesInString( input, options: [], range:NSMakeRange(0, input.characters.count) ) | |
| var results: [String] = [] | |
| for i in 0 ..< matches.count { | |
| results.append( (input as NSString).substringWithRange(matches[i].range) ) | |
| } | |
| return results | |
| } | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment