C#-正则表达式的使用---ShinePans

来源:互联网 发布:ug车床编程视频 编辑:程序博客网 时间:2024/05/21 19:33
//正则表达式分解字符串//Regex:表示不可变的正则表达式//Matches:在输入字符串中搜索正则表达式的所有匹配项并返回所有成功的匹配,类比为多次调用match方法//Groups:获取由正则表达式匹配的集合using System;using System.Text;using System.IO;using System.Security.Cryptography;using System.Text.RegularExpressions; //正则表达式class program{    static string encryptKey = "Fuck";  //加密密匙    static void Main(string[] args)    {        string str = "16:08:30 192.168.1.1 你好 16:09:00 192.168.0.1 大家好";        Regex myRegex = new Regex(@"(?<time>(\d|\:)+)\s" + @"(?<ip>(\d|\.)+)\s" + @"(?<company>\S+)\s"); //对字符串按指定格式进行分解        MatchCollection myMatches = myRegex.Matches(str);        string strNew="";        foreach(Match myMatch in myMatches)        {            strNew+="\n 时间:"+myMatch.Groups["time"].ToString();            strNew+="\n 地址:"+myMatch.Groups["ip"].ToString();            strNew+="\n 再说一句:"+myMatch.Groups["company"].ToString()+"\n";        }        Console.WriteLine(strNew);        Console.ReadLine();    }}




8 0
原创粉丝点击