iOS - regex to match word boundary, including underscore

浪子不回头ぞ 提交于 2021-02-10 13:16:17

问题


I have a regex that I'm trying to run to match a variety of search terms. For example:

the search "old" should match: -> age_old -> old_age but not -> bold - as it's not at the start of the word

To do this, I was using a word boundary. However, word boundary doesn't take into account underscores. As mentioned here, there are work arounds available in other languages. Unfortunately, with NSRegularExpression, this doesn't look possible. Is there any other way to get a word boundary to work? Or other options?


回答1:


Swift and Objective C support ICU regex flavor. This flavor supports look-behinds of fixed and constrained width.

(?= ... )    Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position.

(?! ... )    Negative look-ahead assertion. True if the parenthesized pattern does not match at the current input position. Does not advance the input position.

(?<= ... )    Look-behind assertion. True if the parenthesized pattern matches text preceding the current input position, with the last character of the match being the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.)

(?<! ... )    Negative Look-behind assertion.

So, you can use

 let regex = "(?<![\\p{L}\\d])old(?![\\p{L}\\d])";

See regex demo

Here is a Swift code snippet extracting all "old"s:

func matchesForRegexInText(regex: String, text: String) -> [String] {

    do {
        let regex = try NSRegularExpression(pattern: regex, options: [])
        let nsString = text as NSString
        let results = regex.matchesInString(text,
            options: [], range: NSMakeRange(0, nsString.length))
        return results.map { nsString.substringWithRange($0.range)}
    } catch let error as NSError {
        print("invalid regex: \(error.localizedDescription)")
        return []
    }
}

let s = "age_old -> old_age but not -> bold"
let rx = "(?<![\\p{L}\\d])old(?![\\p{L}\\d])"
let matches = matchesForRegexInText(rx, text: s)
print(matches) // => ["old", "old"]


来源:https://stackoverflow.com/questions/33767112/ios-regex-to-match-word-boundary-including-underscore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!