zoukankan      html  css  js  c++  java
  • C#_正则表达式

     1 //2.用来描述字符串特征的表达式     特征:必须出现的内容   可能出现的内容 不能出现的内容
     2             //观察字符串规律 根据规律总结特征 然后根据特定字符串的特征来编写正则表达式
     3 
     4             //[采集器]  URLRewite   Validator
     5 
     6 
     7             //元字符:
     8             //   .       表示除了
    之外任意的单个字符   a.b          
     9             //   []     表示字符的 筛选    a[xyz]b     表示ab之间  只能是[]中任意一个   a[0-9a-z]b    a[^0-9]b只能出现除了0-9之外的任意一个字符
    10             //    |       表示或的意思   | 优先级低  z|food   不匹配zood   (z|f)ood这个才匹配
    11             //   ()         改变优先级  表达式定义为“组”(group)
    12             //   *        限定符   a.*b   表示前面的表达式出现0次或多次   zoo*   匹配 zo   zooooo    // (zoo)* 匹配 zoozoo
    13             //  +       限定符 ,表示前面的表达式必须出现1次或多次 至少一次
    14             //  ?         限定符   必须出现0次或者一次   终止贪婪模式    正则表达式默认贪婪的
    15 
    16             //a[0-9]+b      a0b√   a89b√   ab×          d 等价于0-9     a[0-9]b  adb    digital      
    17 
    18             //D   [^0-9]
    19             //s   空白符
    20             //S    除了s之外的所有字符
    21             //w   [0-9a-zA-Z_]         word
    22             //W  除了w之外的所有字符
    23 
    24             // 表示单词的边界   断言
    25 
    26 
    27             //{n}           限定前面的表达式必须出现n次
    28             //a[0-9]{10}b       a1234567890b√   必须是10次
    29 
    30             //{n,}          限定前面的表达式至少出现n次
    31 
    32 
    33             //{n,m}         限定前面的表达式至少出现n次、至多出现m次
    34             //a[0-9]{3,5}b    a2222b√    a23b×
    35 
    36 
    37             //^  表示字符串的开头   $   表示字符串的结尾                字符串的两个特征   断言:值判断 不匹配
    38 
    39             //^abc      abcasdfas√      acbdsfas×       描述字符串开头必须是abc
    40 
    41             //xyz$     asfsowerojflxyz√   asidfoljslfyz×   描述字符串结尾必须是xyz
    42 
    43             //      a[sS]b            a[wW]b   a[dD]b
    44 
    45             #endregion
    46 
    47             #region >10  and <=20的所有整数
    48 
    49             //Regex.IsMatch();//判断是否匹配
    50             //Regex.Match();//提取某一个匹配
    51             //Regex.Matches();//提取所有匹配
    52             //Regex.Replace();//替换
    53             //Regex.Split();//分割
    54 
    55 
    56             //1.正则表达式第一步  观察字符串找规律 根据规律来写正则
    57             //while (true)
    58             //{
    59             //    Console.WriteLine("Please input:");
    60             //    string s = Console.ReadLine();
    61             //    bool b=Regex.IsMatch(s, "^(1[1-9]|20)$");           //正则
    62             //    Console.WriteLine(b);
    63             //}
    正则定义
    Case1: 判断用户六位数字密码输入:
    bool  b = Regex.IsMatch(inputMsg,"^[0-9]{6}$");
     
    Case2:判断输入的邮件格式是否正确:
      1.首先字符串中必须包含@ 和  .
      2.不能以@和.开头或者结尾
      3.@必须在最后一个.之前  
        Code:
     1       
     2 //private static bool IsEmailOrNot(string email)
     3 //{
     4 // if (email.Contains('@') && email.Contains('.'))
     5 // {
     6 // if (email.StartsWith("@") || email.StartsWith(".") || email.EndsWith("@") || email.EndsWith("."))
     7 // {
     8 // return false;
     9 // }
    10 // else
    11 // {
    12 // if (email.LastIndexOf('@') < email.LastIndexOf('.'))
    13 // {
    14 // return true;
    15 // }
    16 // else 
    17 // {
    18 // return false;
    19 // }
    20 // }
    21 // }
    22 // else return false;
    23 //}
    判断邮件
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Text.RegularExpressions;
      6 using System.Threading.Tasks;
      7 
      8 namespace _006正则练习
      9 {
     10     class Program
     11     {
     12         static void Main(string[] args)
     13         {
     14             //bool b1 = Regex.IsMatch("bbbbg", "^b.*g$");             //true
     15             //bool b2 = Regex.IsMatch("bg","^b.*g$");                     //true
     16             //bool b3 = Regex.IsMatch("gege","^b.*g$");                   //false
     17             //Console.WriteLine("{0} {1} {2}",b1,b2,b3);
     18 
     19 
     20             #region 判断邮政编码
     21 
     22             //while (true)
     23             //{
     24             //    Console.WriteLine("Please input:");
     25 
     26             //    string postCode = Console.ReadLine();
     27             //    bool b = Regex.IsMatch(postCode,"^[0-9]{6}$");
     28 
     29 
     30             //    bool b2 = Regex.IsMatch(postCode,"^\d{6}$");
     31 
     32             //    //默认.net 采用   Unicode字符匹配 中文数字和英文数字匹配
     33             //    bool b3 = Regex.IsMatch(postCode, @"^d{6}$",RegexOptions.ECMAScript);  //使用RegexOptions.ECMAScript选项后  就不匹配中文数字了
     34 
     35 
     36             //    Console.WriteLine(b);
     37             //}
     38 
     39             #endregion
     40 
     41 
     42             #region 假设 1.18位都是数字  或者前17位为数字,最后为x或者X   2.15位都是数字
     43 
     44             //while (true)
     45             //{
     46             //    Console.WriteLine("Please Input a number:");
     47             //    string s = Console.ReadLine();
     48 
     49             //    bool b = Regex.IsMatch(s, "^([0-9]{18})$|^([0-9]{17}[xX])$|^([0-9]{15})$");
     50             //    bool b2 = Regex.IsMatch(s, "^([0-9]{15}|[0-9]{17}[0-9xX])$");
     51             //    bool b3 = Regex.IsMatch(s,"^[0-9]{15}([0-9]{2}[0-9xX])?$");
     52 
     53 
     54             //    Console.WriteLine(b);
     55 
     56             //}
     57 
     58 
     59 
     60             
     61             #endregion
     62 
     63             #region 区号-电话 1.  区号3位 或4位       2.电话7位或8位     3.-可有可无    4.所有的5为电话号  5.所有的11位电话号
     64 
     65             //while (true)
     66             //{
     67             //    Console.WriteLine("Please Input a number:");
     68             //    string s = Console.ReadLine();
     69 
     70                 
     71             //    bool b = Regex.IsMatch(s, @"^(d{3}-?d{8}|d{4}-?d{7}|d{5}|d{11})$");
     72 
     73             //    bool b2 = Regex.IsMatch(s, @"^(d{3,4}-?d{7,8}|d{5})$", RegexOptions.ECMAScript);
     74 
     75             //}
     76 
     77 
     78              
     79 
     80 
     81 
     82             #endregion
     83 
     84             #region 邮件地址
     85 
     86 
     87 
     88 
     89             //while (true)
     90             //{
     91             //    Console.WriteLine("Please Input  you email:");
     92 
     93             //    string email = Console.ReadLine();
     94 
     95 
     96             //    //.在[] 中表示普通的.   不需要转义   - 需要转义
     97             //    bool b=Regex.IsMatch(email,@"^[a-zA-Z0-9._-]+@[0-9a-zA-Z-]+(.[0-9a-zA-Z]){1,}$");
     98 
     99 
    100 
    101             //    bool b2 = Regex.IsMatch(email,@"^w+@w+(.w+){1,}$",RegexOptions.ECMAScript);
    102             //}
    103 
    104 
    105 
    106 
    107 
    108             #endregion
    109 
    110 
    111             #region  验证ip 4段: 分割最多三位数字
    112 
    113             //192.168.15.12     333.333.55.335
    114 
    115             //while (true)
    116             //{
    117             //    Console.WriteLine("Please input a ip Address:");
    118             //    string ip = Console.ReadLine();
    119 
    120             //    bool b = Regex.IsMatch(ip, @"^d{1,3}.d{1,3}.d{1,3}.d{1,3}$");
    121 
    122             //    Console.WriteLine(b);
    123 
    124             //}
    125 
    126 
    127             #endregion
    128 
    129             #region 判断日期是否合法四位数字-两位数字-两位数字
    130 
    131             while (true)
    132             {
    133                 Console.WriteLine("Please input a  date:");
    134                 string date = Console.ReadLine();
    135 
    136                 bool b = Regex.IsMatch(date, @"^d{4}-(0[1-9]|1[0-2])-([0-2][])$");
    137 
    138             }
    139 
    140 
    141 
    142 
    143 
    144             #endregion
    145 
    146         }
    147     }
    148 }
  • 相关阅读:
    二叉排序树的最低公共祖先
    [jobdu]树中两个结点的最低公共祖先
    [jobdu]用两个栈实现队列
    [leetcode]Balanced Binary Tree
    [jobdu]从尾到头打印链表
    [leetcode]Flatten Binary Tree to Linked List
    [leetcode]Unique Binary Search Trees
    hdu 4059
    hdu 3972 1 M possible
    CF 317D Game with Powers
  • 原文地址:https://www.cnblogs.com/siyi/p/4972173.html
Copyright © 2011-2022 走看看