Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写一些正则表达式来查找子域并替换Swift中的值_Swift_Regex - Fatal编程技术网

如何编写一些正则表达式来查找子域并替换Swift中的值

如何编写一些正则表达式来查找子域并替换Swift中的值,swift,regex,Swift,Regex,我有一个场景,在这个场景中,我可以在API响应中使用的遗留URL属性的子域不正确 例如,https://profiles.foobar.com实际上应该是https://profiles-v2.foobar.com 这与我收到的所有遗留URL相同,子域需要更新以包括-v2 数据最终将被更正,但是现在我必须支持旧格式和新格式,所以我应该在更新之前检查它是否错误 我想写一个简单的助手函数,如果它不正确,我可以在我的模型中用更新的道具替换子域 现在我在我的每个模型中都有这个,但是对于每个可能的子域,它

我有一个场景,在这个场景中,我可以在API响应中使用的遗留
URL
属性的子域不正确

例如,
https://profiles.foobar.com
实际上应该是
https://profiles-v2.foobar.com

这与我收到的所有遗留
URL
相同,子域需要更新以包括
-v2

数据最终将被更正,但是现在我必须支持旧格式和新格式,所以我应该在更新之前检查它是否错误

我想写一个简单的助手函数,如果它不正确,我可以在我的模型中用更新的道具替换子域

现在我在我的每个模型中都有这个,但是对于每个可能的子域,它基本上是重复的,并且在每个模型中都有

private func mapV2ProfileDomain(\uurl:url?)->url?{
guard let urlString=url?.absoluteString else{return nil}
让mappedString=urlString.replacingOccurrences(共:https://profiles.,带有:https://profiles-v2.")
返回URL(字符串:mappedString)
}

使用正则表达式可以实现这一点吗

实际上您已经使用了正确的方法,
replacingOccurrences
可以通过将
options
参数设置为
.regularExpression

let str2 = str.replacingOccurrences(
    of: #"https://(\w*)\."#,
    with: "https://$1-v2.",
    options: .regularExpression
)

实际上,您已经使用了正确的方法,
replacingOccurrences
可以通过将
options
参数设置为
.regularExpression

let str2 = str.replacingOccurrences(
    of: #"https://(\w*)\."#,
    with: "https://$1-v2.",
    options: .regularExpression
)