gpt4 book ai didi

ios - 用属性字符串和文本替换正则表达式匹配

转载 作者:可可西里 更新时间:2023-11-01 00:36:54 28 4
gpt4 key购买 nike

我们的应用程序 Api 返回一个具有自定义格式的字段,用于用户提及,就像:“这是提及 @(steve|user_id) 的文本”。所以在 UITextView 上显示它之前,需要处理文本,找到模式并替换为更用户友好的内容。最终结果将是“这是一个提及@steve 的文本”,其中@steve 应该有一个带有user_id 的链接属性。与 Facebook 基本相同的功能。

首先,我创建了一个 UITextView 扩展,具有正则表达式模式的匹配功能。

extension UITextView {
func processText(pattern: String) {
let inString = self.text
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSMakeRange(0, inString.characters.count)
let matches = (regex?.matchesInString(inString, options: [], range: range))! as [NSTextCheckingResult]

let attrString = NSMutableAttributedString(string: inString, attributes:attrs)

//Iterate over regex matches
for match in matches {
//Properly print match range
print(match.range)

//A basic idea to add a link attribute on regex match range
attrString.addAttribute(NSLinkAttributeName, value: "\(schemeMap["@"]):\(must_be_user_id)", range: match.range)

//Still text it's in format @(steve|user_id) how could replace it by @steve keeping the link attribute ?
}
}
}

//To use it
let regex = ""\\@\\(([\\w\\s?]*)\\|([a-zA-Z0-9]{24})\\)""
myTextView.processText(regex)

这就是我现在拥有的,但我一直在尝试获得最终结果

非常感谢!

最佳答案

我稍微更改了您的正则表达式,但得到了很好的结果。也稍微修改了代码,所以你可以直接在 Playgrounds 中测试它。

func processText() -> NSAttributedString {
let pattern = "(@\\(([^|]*)([^@]*)\\))"
let inString = "this is a text with mention for @(steve|user_id1) and @(alan|user_id2)."
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSMakeRange(0, inString.characters.count)
let matches = (regex?.matchesInString(inString, options: [], range: range))!

let attrString = NSMutableAttributedString(string: inString, attributes:nil)
print(matches.count)
//Iterate over regex matches
for match in matches.reverse() {
//Properly print match range
print(match.range)

//Get username and userid
let userName = attrString.attributedSubstringFromRange(match.rangeAtIndex(2)).string
let userId = attrString.attributedSubstringFromRange(match.rangeAtIndex(3)).string

//A basic idea to add a link attribute on regex match range
attrString.addAttribute(NSLinkAttributeName, value: "\(userId)", range: match.rangeAtIndex(1))

//Still text it's in format @(steve|user_id) how could replace it by @steve keeping the link attribute ?
attrString.replaceCharactersInRange(match.rangeAtIndex(1), withString: "@\(userName)")
}
return attrString
}

关于ios - 用属性字符串和文本替换正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35556355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com