Help us understand the problem. What is going on with this article?

Swift2.0で正規表現

More than 3 years have passed since last update.

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)

以上です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away