在 Swift 中的字符串中删除 link 的正则表达式
Regular expression to remove link in String in Swift
我有一堆从服务中收到的字符串,需要更改文本以提取和删除 3 种类型的 link,如下所述
anchor - [anchor:info]Account Details[/anchor]
action - [action:contact]Contact info[/anchor]
link-to - [link-to:register]Create An Account[/link-to]
来自服务的全长文本示例:
- "There's a problem with your
[anchor:info]Account Details[/anchor]
."
- "There's a problem with your
[anchor:rewards]Sign Up For Rewards[/anchor]
."
- "We didn't recognize this account. Please re-enter your email or
[link-to:register]Create An Account[/link-to]
."
预期结果应该是:
- "There's a problem with your Account Details."
- "There's a problem with your Sign Up For Rewards."
- "We didn't recognize this account. Please re-enter your email or Create An Account."
我想我会使用 replacingOccurrences 函数来实现这一点。但是我还没有破解我需要的格式的正则表达式。
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: "regex here", with: " ", options: .regularExpression, range: nil)
我可以使用 3 个单独的正则表达式来匹配这 3 个案例,或者使用一个可以处理以下内容的正则表达式:
[any_link_type:any_identifier]Any Text[/any_link_type]
一些正则表达式专家可以帮我解决这个问题吗?
知道了 :) 这个正则表达式可以满足我的要求:\[.*?\]
let linkString = "We didn't recognize this account. Please re-enter your email or [link-to:register]Create An Account[/link-to]."
let newLinkString = linkString.replacingOccurrences(of: "\[.*?\]", with: "", options: .regularExpression, range: nil)
试试这个模式^([^[]+)\[([^:\]]+)[^\]]*\]([^[]+)\[\/\]
并替换为</code>。</p>
<p>解释:</p>
<p><code>^
- 字符串的开头
([^[]+)
- 匹配 [
以外的一个或多个字符并存储在捕获组
\[
- 按字面意思匹配 [
([^:\]]+)
- 匹配 :
或 ]
以外的一个或多个字符并存储在捕获组
[^\]]*
- 匹配 ]
以外的零个或多个字符
\]
- 按字面意思匹配 ]
([^[]+)
- 匹配 [
以外的一个或多个字符并存储在捕获组
\[\/
- 按字面意思匹配 [/
</code> - 匹配与第二个捕获组中匹配的相同文本(因此它匹配结束标记,如 <code>anchor
)
\]
- 按字面意思匹配 ]
使用以下代码获得预期的输出。
Swift 4:-
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: "\[.*?\]", with: "", options: .regularExpression, range: nil)
print(newString) //There's a problem with your Account Details.
Swift 5:-
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: #"\[.*?\]"#, with: " ", options: .regularExpression, range: nil)
print(newString) //There's a problem with your Account Details.
我有一堆从服务中收到的字符串,需要更改文本以提取和删除 3 种类型的 link,如下所述
anchor - [anchor:info]Account Details[/anchor]
action - [action:contact]Contact info[/anchor]
link-to - [link-to:register]Create An Account[/link-to]
来自服务的全长文本示例:
- "There's a problem with your
[anchor:info]Account Details[/anchor]
." - "There's a problem with your
[anchor:rewards]Sign Up For Rewards[/anchor]
." - "We didn't recognize this account. Please re-enter your email or
[link-to:register]Create An Account[/link-to]
."
预期结果应该是:
- "There's a problem with your Account Details."
- "There's a problem with your Sign Up For Rewards."
- "We didn't recognize this account. Please re-enter your email or Create An Account."
我想我会使用 replacingOccurrences 函数来实现这一点。但是我还没有破解我需要的格式的正则表达式。
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: "regex here", with: " ", options: .regularExpression, range: nil)
我可以使用 3 个单独的正则表达式来匹配这 3 个案例,或者使用一个可以处理以下内容的正则表达式:
[any_link_type:any_identifier]Any Text[/any_link_type]
一些正则表达式专家可以帮我解决这个问题吗?
知道了 :) 这个正则表达式可以满足我的要求:\[.*?\]
let linkString = "We didn't recognize this account. Please re-enter your email or [link-to:register]Create An Account[/link-to]."
let newLinkString = linkString.replacingOccurrences(of: "\[.*?\]", with: "", options: .regularExpression, range: nil)
试试这个模式^([^[]+)\[([^:\]]+)[^\]]*\]([^[]+)\[\/\]
并替换为</code>。</p>
<p>解释:</p>
<p><code>^
- 字符串的开头
([^[]+)
- 匹配 [
以外的一个或多个字符并存储在捕获组
\[
- 按字面意思匹配 [
([^:\]]+)
- 匹配 :
或 ]
以外的一个或多个字符并存储在捕获组
[^\]]*
- 匹配 ]
\]
- 按字面意思匹配 ]
([^[]+)
- 匹配 [
以外的一个或多个字符并存储在捕获组
\[\/
- 按字面意思匹配 [/
</code> - 匹配与第二个捕获组中匹配的相同文本(因此它匹配结束标记,如 <code>anchor
)
\]
- 按字面意思匹配 ]
使用以下代码获得预期的输出。
Swift 4:-
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: "\[.*?\]", with: "", options: .regularExpression, range: nil)
print(newString) //There's a problem with your Account Details.
Swift 5:-
let aString = "There's a problem with your [anchor:info]Account Details[/anchor]."
let newString = aString.replacingOccurrences(of: #"\[.*?\]"#, with: " ", options: .regularExpression, range: nil)
print(newString) //There's a problem with your Account Details.