【发布时间】:2017-06-30 10:36:06
【问题描述】:
我有一个来自数据库的字符串,其中包含一些 url。我正在尝试用新网址替换旧网址。因为包含 url 的字符串的格式是 {s:5:"hello"},所以我还需要用新的字符串长度替换旧的字符串长度。
我试过了
var url = "http://localhost/arearaf";
var text = "somelongstring; 35; '%site_url%'; s:46:\"%site_url%/wp-content/uploads/2017/06/logo.png\"; someotherlongstring; s:54:\"%site_url%/wp-content/uploads/2017/06/logo-150x150.png\";";
var result = Regex.Replace(text, @"s:\d+:\" + "\"%site_url%(.*?)\";", "s:" + (url.Length + "$1".Length) + @":\" + "\"" + url + "$1" + @"\" + "\";");
导致
"somelongstring; 35; '%site_url%'; s:26:\"http://localhost/arearaf/wp-content/uploads/2017/06/logo.png\"; someotherlongstring; s:26:\"http://localhost/arearaf/wp-content/uploads/2017/06/logo-150x150.png\";"
但应该是
"somelongstring; 35; '%site_url%'; s:60:\"http://localhost/arearaf/wp-content/uploads/2017/06/logo.png\"; someotherlongstring; s:68:\"http://localhost/arearaf/wp-content/uploads/2017/06/logo-150x150.png\";"
"$1".Length 显然不会返回 "/wp-content/uploads/2017/06/logo.png".Length。如何获取每个替换的捕获组的长度?我是否也正确处理了这个问题?如果没有,我该怎么办?
这种格式
{s:5:"hello"}不是有名字什么的吗?我不能谷歌它。我能找到的最接近的东西是 CMS,它代表内容管理系统,我猜?不过,还不够。
【问题讨论】: