zoukankan      html  css  js  c++  java
  • c#正则提取和替换

    1. /// <summary>  
    2. /// 正则表达式提取和替换内容  
    3. /// </summary>  
    4. public static string Contentextract()  
    5. {  
    6.     string result = "";  
    7.     string str = "大家好! <User EntryTime='2010-10-7' Email='zhangsan@163.com'>张三</User> 自我介绍。";  
    8.     Regex regex = new Regex(@"<Users*EntryTime='(?<time>[sS]*?)'s+Email='(?<email>[sS]*?)'>(?<userName>[sS]*?)</User>", RegexOptions.IgnoreCase);  
    9.     Match match = regex.Match(str);  
    10.     if(match.Success)  
    11.     {  
    12.         string userName = match.Groups["userName"].Value;   //获取用户名  
    13.         string time = match.Groups["time"].Value;           //获取入职时间  
    14.         string email = match.Groups["email"].Value;         //获取邮箱地址  
    15.         string strFormat = String.Format("我是:{0},入职时间:{1},邮箱:{2}", userName, time, email);  
    16.         result = regex.Replace(str, strFormat);    //替换内容  
    17.         Console.WriteLine(result);    
    18.     }  
    19.     return result;   //结果:大家好!我是张三,入职时间:2010-10-7,邮箱:zhangsan@163.com 自我介绍。  
  • 相关阅读:
    继承与多态,Instanceof关键字
    面向对象,单例模式
    方法
    数组
    流程控制
    基础语法
    连接linux四大远程工具
    MYSQL-索引的设计
    银行一类(Ⅰ类)、二类(Ⅱ类)、三类(Ⅲ类)账户区别是什么?
    真正有效的学习
  • 原文地址:https://www.cnblogs.com/zhangweixin/p/4071687.html
Copyright © 2011-2022 走看看