代码片断:利用正则替换匹配的字符串

代码片断:利用正则表达式替换文本中匹配的字符串

private string GetNewString(string str_old)
    {
        string str_new = string.Empty;
        string pattern = @"http://www.mysite/blog/photo/\w+/\w+/[a-zA-z0-9_\-]+";
        MatchCollection matchs = Regex.Matches(str_old, pattern);
        foreach (Match match in matchs)
        {
            str_new = match.Value.Replace("www.mysite/blog/photo", "hr.mysite.com/blog");
            str_new = str_new.Replace("o_", "w_");
            str_old = str_old.Replace(match.Value, str_new);
        }
        return str_old;
    }


你可能感兴趣的:(代码片断:利用正则替换匹配的字符串)