regex.matches 方法匹配和计数错误

The regex.matches method is matching and counting wrongly

在 TextView 中,用户可以插入文本和图像,例如注释。为了将整个 TextView 内容保存在数据库 (Realm) 中,我将图像本身替换为模式“[image]imageName[/image]”,因此当我将此数据加载回 TextView 时,我想替换回此模式对于图像。我做了这个功能:

let attributedString = NSMutableAttributedString(string: txtNote.text)

        let range = NSRange(location: 0, length: attributedString.string.utf16.count)
        let regex = NSRegularExpression("[image](.*?)[/image]")

        for match in regex.matches(in: attributedString.string, options: [], range: range) {
            if let rangeForImageName = Range(match.range(at: 1), in: attributedString.string){

                let imageName = String(attributedString.string[rangeForImageName])

                if let image = loadImage(named: imageName) {

                    let attachment = NSTextAttachment()
                    attachment.image = image
                    let oldWidth = attachment.image!.size.width;
                    let scaleFactor = (oldWidth / (txtNote.frame.size.width - 10))

                    attachment.image = UIImage(cgImage: attachment.image!.cgImage!, scale: scaleFactor, orientation: .up)

                    let attString = NSAttributedString(attachment: attachment)

                    txtNote.textStorage.insert(attString, at: txtNote.selectedRange.location)
                } else {
                    print("Image not found")
                }
            }
        }

我也有这个扩展来避免上面函数中的 try catch:

extension NSRegularExpression {
    convenience init(_ pattern: String) {
        do {
            try self.init(pattern: pattern)
        } catch {
            preconditionFailure("Illegal regular expression: \(pattern).")
        }
    }
}

我是运行的例子,attributedString上的内容:

Like Gorillaz :D
[image]4397ACA6-ADDC-4977-8D67-9FF44F10384A.jpeg[/image]

[image]9BE22CA8-9C6C-4FF9-B46F-D8AF33703061.jpeg[/image]

Etc.{
}

应该是2个匹配,镜像名称应该是:“4397ACA6-ADDC-4977-8D67-9FF44F10384A.jpeg”和“9BE22CA8-9C6C-4FF9-B46F-D8AF33703061.jpeg”。

但我的函数返回 14 个匹配项,图像名称如下:"k"、"ll"、""、"]4397ACA6-ADDC-4977-8D67-9FF44F10384A.jp", “[”等

知道我做错了什么吗?我整天都在研究这样的错误,但没有成功。

[image][/image]形式字符类匹配单个字符,前者匹配image,后者也匹配 /.

如果您想将正则表达式的一部分视为文字子字符串,您可以 "quote" 使用 \Q...\E 运算符:

let regex = NSRegularExpression("\Q[image]\E(.*?)\Q[/image]\E")

如果您确定自己在做什么,请手动转义括号,"\[image\](.*?)\[/image\]"

参见 Regular Expression Metacharacters table:

\Q   Quotes all following characters until \E.
\E   Terminates a \Q ... \E quoted sequence.

"Quotes"在这里表示"adds backslashes before the special chars to make them match as literal chars"。