- /// <summary>
- /// 正则表达式提取和替换内容
- /// </summary>
- public static string Contentextract()
- {
- string result = "";
- string str = "大家好! <User EntryTime='2010-10-7' Email='zhangsan@163.com'>张三</User> 自我介绍。";
- Regex regex = new Regex(@"<Users*EntryTime='(?<time>[sS]*?)'s+Email='(?<email>[sS]*?)'>(?<userName>[sS]*?)</User>", RegexOptions.IgnoreCase);
- Match match = regex.Match(str);
- if(match.Success)
- {
- string userName = match.Groups["userName"].Value; //获取用户名
- string time = match.Groups["time"].Value; //获取入职时间
- string email = match.Groups["email"].Value; //获取邮箱地址
- string strFormat = String.Format("我是:{0},入职时间:{1},邮箱:{2}", userName, time, email);
- result = regex.Replace(str, strFormat); //替换内容
- Console.WriteLine(result);
- }
- return result; //结果:大家好!我是张三,入职时间:2010-10-7,邮箱:zhangsan@163.com 自我介绍。
- }