29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift2.0で正規表現

Last updated at Posted at 2015-10-11

Swiftで正規表現扱うときどうやるんだろうと思って調べてるうち行き着いた先が「EasyRamble」さんの以下の記事です。
http://easyramble.com/swift-regular-expression-utility.html

ただSwift2.0になって少し変わっているようなので、手を加えました。

init

変更前

init
init(_ pattern: String) {
    self.pattern = pattern
    var error: NSError?
    self.internalRegexp = NSRegularExpression( pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive, error: &error)!
}

変更後

NSRegularExpressionの引数にエラーが含まれなくなったようです。

なので以下のようにtry catchへ変更しました。

init
init(_ pattern: String) {
    self.pattern = pattern
    do {
        self.internalRegexp = try NSRegularExpression(pattern: pattern, options: [])
    } catch let error as NSError {
        print(error.localizedDescription)
        self.internalRegexp = NSRegularExpression()
    }
}

isMatch

変更前

isMatch
func isMatch(input: String) -> Bool {
    let matches = self.internalRegexp.matchesInString( input, options: nil, range:NSMakeRange(0, count(input)) )
    return matches.count > 0
}

変更後

optionsの指定方法ぐらいですね。

isMatch
func isMatch(input: String) -> Bool {
    let nsString = input as NSString
    let matches = self.internalRegexp.matchesInString(input, options:[], range:NSMakeRange(0, nsString.length))
    return matches.count > 0
}

matches

変更前

matches
func matches(input: String) -> [String]? {
    if self.isMatch(input) {
        let matches = self.internalRegexp.matchesInString( input, options: nil, range:NSMakeRange(0, count(input)) )
        var results: [String] = []
        for i in 0 ..< matches.count {
            results.append( (input as NSString).substringWithRange(matches[i].range) )
        }
        return results
    }
    return nil
}

変更後

countの箇所でエラーが起きるので以下のように変更しています。

matches
func matches(input: String) -> [String]? {
    if self.isMatch(input) {
        let nsString = input as NSString
        let matches = self.internalRegexp.matchesInString( input, options: [], range:NSMakeRange(0, nsString.length) )
        var results: [String] = []
        for i in 0 ..< matches.count {
            results.append( (input as NSString).substringWithRange(matches[i].range) )
        }
        return results
    }
    return nil
}

個人的に追加

正規表現で一致した内容を削るものです。

delMatches
func delMatches(input:String) -> String {
    var strRet:String = input
    if self.isMatch(input) {
        let matchList = self.matches(input)
        for var i = 0; i < matchList!.count; i++ {
            strRet = strRet.stringByReplacingOccurrencesOfString(matchList![i], withString: "", options: [], range: nil)
        }
    }
    return strRet
}

使い方

左側に正規表現を記載し、右側に対象の文字列を書きます。

Regexp("(\\d+)").delMatches(self._strTitle)

以上です。

29
27
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?