Swift 正则表达式不起作用 [英] Swift Regex doesn't work

查看:90
本文介绍了Swift 正则表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下扩展方法来获取子字符串的 NSRange 数组:

I am using the following extension method to get NSRange array of a substring:

extension String {
  func nsRangesOfString(findStr:String) -> [NSRange] {
    let ranges: [NSRange]
    do {
      // Create the regular expression.
      let regex = try NSRegularExpression(pattern: findStr, options: [])

      // Use the regular expression to get an array of NSTextCheckingResult.
      // Use map to extract the range from each result.
      ranges = regex.matches(in: self, options: [], range: NSMakeRange(0, self.characters.count)).map {$0.range}
    }
    catch {
      // There was a problem creating the regular expression
      ranges = []
    }
    return ranges
  }
}

但是,我没有意识到为什么它有时不起作用.这里有两种类似的情况,一种有效,另一种无效:

However, I didn't realize why it doesn't work sometimes. Here are two similar cases, one works and the other doesn't:

那个有效:

自我(字符串):

"וצפן (קרי: יִצְפֹּ֣ן) לַ֭יְשָׁרִים תּוּשִׁיָּ֑ה מָֹּהמָֹּהמְָֹתכחֵֹתתלֹן

"וצפן (קרי: יִצְפֹּ֣ן) לַ֭יְשָׁרִים תּוּשִׁיָּ֑ה מָ֝גֵ֗ן לְהֹ֣לְכֵי תֹֽם׃"

findStr:

"קרי:"

那个没有:

自我(字符串):

"לִ֭נְצֹר אָרְח֣וֹת מִשְׁפָּ֑ט וְדֶ֖רֶךְחסידוחסידו(קִידוחסידואסידו(קִידוחסידואקידוורידוותתתמִשְׁפָּ֑ט לִ֭נְצֹר אָרְח֣וֹת)

"לִ֭נְצֹר אָרְח֣וֹת מִשְׁפָּ֑ט וְדֶ֖רֶךְ חסידו (קרי: חֲסִידָ֣יו) יִשְׁמֹֽר׃"

findStr:

"קרי:"

(不过,另一种稳定的方法是合适的答案.)

(An alternate steady method would be an appropriate answer though.)

推荐答案

NSRange 范围以 UTF-16 代码单元(其中是 NSString 内部使用的),因此长度必须是self.utf16.count:

NSRange ranges are specified in terms of UTF-16 code units (which is what NSString uses internally), therefore the length must be self.utf16.count:

        ranges = regex.matches(in: self, options: [],
                               range: NSRange(location: 0, length: self.utf16.count))
            .map {$0.range}

对于您的第二个字符串,我们有

In the case of your second string we have

let s2 = "לִ֭נְצֹר אָרְח֣וֹת מִשְׁפָּ֑ט וְדֶ֖רֶךְ חסידו (קרי: חֲסִידָ֣יו) יִשְׁמֹֽר׃"
print(s2.characters.count) // 46
print(s2.utf16.count)      // 74

这就是为什么在您的代码中找不到该模式的原因.

and that's why the pattern is not found with your code.

Swift 4 开始,您还可以为整个字符串计算 NSRange

Starting with Swift 4 you can compute a NSRange for the entire string also as

NSRange(self.startIndex..., in: self)

这篇关于Swift 正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆