文章标题

来源:互联网 发布:最大的淘宝客网站 编辑:程序博客网 时间:2024/04/30 10:11

Regex
正则表达式通常用来检查,检索,替换符合某个格式的文本

元字符:
正则表达式语言由两种基本字符组成.
原义(正常)文本字符和元字符.
元字符使用正则表达式具有处理能力.

   ^        表示开头   $         表示结尾   .         匹配除换行符以外的任意字符   *         0 or more   ?         0 or 1   +         1 or more  \w         表示字母,数字,下划线,汉字任意字符(指大小写字母,0-9的数字,下划线“_”)  \W       \w 的补集  \d         表示数字  \D         表示非数字  \s         表示字符串    \S         表示非空白字符  [\s\S]     表示任意字符  [\s\S]*     表示0到任意个字符  [\s\S]*?   0个字符,匹配任意个字符前的次数  [a-z]       表示某个范围内的字符,与指定区间内任何字符进行匹配  [A-Z]     [0-9]  \u4e00-\u9fa5   表示中文  |           逻辑或  ()         分组  {n,m}       表示最少匹配n次,最高匹配m次  {n,}       最少匹配n次  {n}         匹配前面的字符最少n次  [^X]       表示除了X外任意字符
using System;using System.Text.RegularExpressions;using System.Collections;using System.Collections.Generic;namespace 正则表达式{    class MainClass    {        public static void Main (string[] args)        {            //演示在开头处拼接//          string str = "大家好";//          string newStr = Regex.Replace (str, "^", "首长:");//          Console.WriteLine (newStr);            //演示在结尾处拼接//          string str = "大家好";//          string newStr = Regex.Replace (str, "$", ",首长");//          Console.WriteLine (newStr);            //以数字开头,中间有n个数字,以中文结尾.//          string str = "010789你";//          string pattern = @"\d*[\u4e00-\u9fa5]$";//          if (Regex.IsMatch (str, pattern)) {//              Console.WriteLine ("合法");//          }else{//              Console.WriteLine ("不合法");//          }            //匹配邮箱            //xxxx@xxx.xx//          string str = "zhaoqingyu@lanou3g.com.cn";//          string pattern = @"\w+@\w+(\.\w+)+$";//          if (Regex.IsMatch (str, pattern)) {//              Console.WriteLine ("合法");//          } else {//              Console.WriteLine ("不合法");//          }            //匹配身份证号            //身份证15位 或 18位            //15位全数字            //18位全数字            //17位数字+X//          string str = "11111111111111111B";   //18位//          string pattern = @"(^\d{15,15}$)|(^\d{17}([0-9]|X)$)";//          if (Regex.IsMatch (str, pattern)) {//              Console.WriteLine ("合法");//          } else {//              Console.WriteLine ("不合法");//          }            //匹配座机            //010-12345678            //01012345678            //(010)12345678//          string tell = "010-12345678";//          string tell = "01012345678";//          string tell = "(010)-1234567";//          string pattern = @"\(0\d{2,3}\)\d{7,8}$|^0\d{2,3}[-]?\d{7,8}$";////            string pattern = @"(^0\d{2,3}[1-9]\d{7,7}$)|(^0\d{2,3}-[1-9]\d{7,7}$)|(^\(0\d{2,3}\)[1-9]\d{7,7}$)";//          if (Regex.IsMatch (tell, pattern)) {//              Console.WriteLine ("合法");//          } else {//              Console.WriteLine ("不合法");//          }            //判断手机号            //138xxxx//          string str = "132456789012";//          string pattern = @"^[1][3,5,7,8][0-9]{9}$";//          if (Regex.IsMatch (str, pattern)) {//              Console.WriteLine ("合法");//          } else {//              Console.WriteLine ("不合法");//          }            // 判断扣扣号            string str = "1322085934";            string pattern = @"^[1,2]\d{9}$";            if (Regex.IsMatch (str, pattern)) {                Console.WriteLine ("合法");            } else {                Console.WriteLine ("不合法");            }            //将#xxx#抽取出来            //#1024#天啊地球要灭亡了,#带着钱一起跑路#带回家按客户的骄傲是多少贷//          string str = "#1024#天啊地球要灭亡了,#带着钱一起跑路#带回家按客户的骄傲是多少贷";//          string pattern = @"#[0-9a-zA-Z\u4e00-\u9fa5]+#";//          MatchCollection mc = Regex.Matches (str, pattern);//          Console.WriteLine (mc.Count);//          foreach (Match item in mc) {//              Console.WriteLine (item);//          }            //检索特殊字符//          string str = "*$172638";//          string pattern = @"[^\w\s]+";//          MatchCollection mc  = Regex.Matches(str,pattern);//          foreach (Match item in mc) {//              Console.WriteLine (item);//          }            //敏感字眼检索//          string str = "天下武功唯快不破,法轮大法真好";//          if (MainClass.JudgeMessage (str)) {//              Console.WriteLine ("消息发送失败");//          } else {//              //上传服务器//              Console.WriteLine("消息发送成功");//          }        }        public static bool JudgeMessage(string s){            string pattern = @"[法论]|[法轮]";            return Regex.IsMatch (s, pattern);          }    }}
0 0
原创粉丝点击