C#中的正则表达式(择一匹配符)

xiaoxiao2021-02-28  39

择一匹配

字符 说明 | 将两个匹配条件进行逻辑“或”(Or)运算。

example

//查找数字或字母 string s = "34((**apple厉害厉害"; string pattern = @"\d|[a-z]"; //匹配数字或者字母 MatchCollection col = Regex.Matches(s,pattern); foreach(Match match in col) Console.Write(match.ToString()); //输出为34apple //将人名输出 //不使用择一匹配 string s = "zhangsan,lisi;wangwu.zhailiu"; string pattern = @"[;,.]"; string[] resArray = Regex.Split(s, pattern); foreach (var s1 in resArray) Console.WriteLine(s1); //使用择一匹配 string s = "zhangsan,lisi;wangwu.zhailiu"; string pattern = @"[;]|[,]|[.]"; string[] resArray = Regex.Split(s, pattern); foreach (var s1 in resArray) Console.WriteLine(s1); //二者的输出结果相同,都为 //zhangsan //lisi //wangwu //zhaoliu
转载请注明原文地址: https://www.6miu.com/read-2620583.html

最新回复(0)