日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Regex类详解

發布時間:2023/12/18 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Regex类详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

    using System;
    using System.Text.RegularExpressions;
    namespace MetarCommonSupport
    {
    /// <summary>
    /// 通過Framwork類庫中的Regex類實現了一些特殊功能數據檢查
    /// </summary>
    public class MetarnetRegex
    {
    ??
    ?? private static MetarnetRegex instance = null;
    ?? public static MetarnetRegex GetInstance()
    ?? {
    ??? if(MetarnetRegex.instance == null)
    ??? {
    ???? MetarnetRegex.instance = new MetarnetRegex();
    ??? }
    ??? return MetarnetRegex.instance;
    ?? }
    ?? private MetarnetRegex()
    ?? {
    ?? }
    ?? /// <summary>
    ?? /// 判斷輸入的字符串只包含漢字
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsChineseCh(string input)
    ?? {
    ??? Regex regex = new Regex("^[/u4e00-/u9fa5]+$");
    ??? return regex.IsMatch(input);
    ?? }

    ?? /// <summary>
    ?? /// 匹配3位或4位區號的電話號碼,其中區號可以用小括號括起來,
    ?? /// 也可以不用,區號與本地號間可以用連字號或空格間隔,
    ?? /// 也可以沒有間隔
    ?? /// /(0/d{2}/)[- ]?/d{8}|0/d{2}[- ]?/d{8}|/(0/d{3}/)[- ]?/d{7}|0/d{3}[- ]?/d{7}
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsPhone(string input)
    ?? {
    ??? string pattern = "^//(0//d{2}//)[- ]?//d{8}$|^0//d{2}[- ]?//d{8}$|^//(0//d{3}//)[- ]?//d{7}$|^0//d{3}[- ]?//d{7}$";
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }
    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否是一個合法的手機號
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsMobilePhone(string input)
    ?? {
    ??? Regex regex = new Regex("^13//d{9}$");
    ??? return regex.IsMatch(input);
    ???
    ?? }


    ?? /// <summary>
    ?? /// 判斷輸入的字符串只包含數字
    ?? /// 可以匹配整數和浮點數
    ?? /// ^-?/d+$|^(-?/d+)(/./d+)?$
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsNumber(string input)
    ?? {
    ??? string pattern = "^-?//d+$|^(-?//d+)(
    //.//d+)?$ ";
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }
    ?? /// <summary>
    ?? /// 匹配非負整數
    ?? ///
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsNotNagtive(string input)
    ?? {
    ??? Regex regex = new Regex(@"^/d+$");
    ??? return regex.IsMatch(input);
    ?? }
    ?? /// <summary>
    ?? /// 匹配正整數
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsUint(string input)
    ?? {
    ??? Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
    ??? return regex.IsMatch(input);
    ?? }
    ?? /// <summary>
    ?? /// 判斷輸入的字符串字包含英文字母
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsEnglisCh(string input)
    ?? {
    ??? Regex regex = new Regex("^[A-Za-z]+$");
    ??? return regex.IsMatch(input);
    ?? }


    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否是一個合法的Email地址
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsEmail(string input)
    ?? {
    ??? string pattern = @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$";
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }


    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否只包含數字和英文字母
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsNumAndEnCh(string input)
    ?? {
    ??? string pattern = @"^[A-Za-z0-9]+$";
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }


    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否是一個超鏈接
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsURL(string input)
    ?? {
    ??? //string pattern = @"
    http://([/w-]+/.)+[/w-]+(/[/w - ./?%&=]*)?";
    ??? string pattern = @"^[a-zA-Z]+://(/w+(-/w+)*)(/.(/w+(-/w+)*))*(/?/S*)?$";
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }


    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否是表示一個IP地址
    ?? /// </summary>
    ?? /// <param name="input">被比較的字符串</param>
    ?? /// <returns>是IP地址則為True</returns>
    ?? public static bool IsIPv4(string input)
    ?? {
    ???
    ??? string[] IPs = input.Split('.');
    ??? Regex regex = new Regex(@"^/d+$");
    ??? for(int i = 0; i<IPs.Length; i++)
    ??? {
    ???? if(!regex.IsMatch(IPs[i]))
    ???? {
    ????? return false;
    ???? }
    ???? if(Convert.ToUInt16(IPs[i]) > 255)
    ???? {
    ????? return false;
    ???? }
    ??? }
    ??? return true;
    ?? }


    ?? /// <summary>
    ?? /// 計算字符串的字符長度,一個漢字字符將被計算為兩個字符
    ?? /// </summary>
    ?? /// <param name="input">需要計算的字符串</param>
    ?? /// <returns>返回字符串的長度</returns>
    ?? public static int GetCount(string input)
    ?? {
    ??? return Regex.Replace(input,@"[/u4e00-/u9fa5/g]","aa").Length;
    ?? }

    ?? /// <summary>
    ?? /// 調用Regex中IsMatch函數實現一般的正則表達式匹配
    ?? /// </summary>
    ?? /// <param name="pattern">要匹配的正則表達式模式。</param>
    ?? /// <param name="input">要搜索匹配項的字符串</param>
    ?? /// <returns>如果正則表達式找到匹配項,則為 true;否則,為 false。</returns>
    ?? public static bool IsMatch(string pattern, string input)
    ?? {
    ??? Regex regex = new Regex(pattern);
    ??? return regex.IsMatch(input);
    ?? }
    ??
    ?? /// <summary>
    ?? /// 從輸入字符串中的第一個字符開始,用替換字符串替換指定的正則表達式模式的所有匹配項。
    ?? /// </summary>
    ?? /// <param name="pattern">模式字符串</param>
    ?? /// <param name="input">輸入字符串</param>
    ?? /// <param name="replacement">用于替換的字符串</param>
    ?? /// <returns>返回被替換后的結果</returns>
    ?? public static string Replace(string pattern, string input, string replacement)
    ?? {
    ??? Regex regex = new Regex(pattern);
    ??? return regex.Replace(input,replacement);
    ?? }

    ?? /// <summary>
    ?? /// 在由正則表達式模式定義的位置拆分輸入字符串。
    ?? /// </summary>
    ?? /// <param name="pattern">模式字符串</param>
    ?? /// <param name="input">輸入字符串</param>
    ?? /// <returns></returns>
    ?? public static string[] Split(string pattern, string input)
    ?? {
    ??? Regex regex = new Regex(pattern);
    ??? return regex.Split(input);
    ?? }
    ?? /// <summary>
    ?? /// 判斷輸入的字符串是否是合法的IPV6 地址
    ?? /// </summary>
    ?? /// <param name="input"></param>
    ?? /// <returns></returns>
    ?? public static bool IsIPV6(string input)
    ?? {
    ??? string pattern = "";
    ??? string temp = input;
    ??? string[] strs = temp.Split(':');
    ??? if(strs.Length > 8)
    ??? {
    ???? return false;
    ??? }
    ??? int count = MetarnetRegex.GetStringCount(input,"::");
    ??? if(count>1)
    ??? {
    ???? return false;
    ??? }
    ??? else if(count == 0)
    ??? {
    ???? pattern = @"^([/da-f]{1,4}:){7}[/da-f]{1,4}$";

    ???? Regex regex = new Regex(pattern);
    ???? return regex.IsMatch(input);
    ??? }
    ??? else
    ??? {
    ???? pattern = @"^([/da-f]{1,4}:){0,5}::([/da-f]{1,4}:){0,5}[/da-f]{1,4}$";
    ???? Regex regex1 = new Regex(pattern);
    ???? return regex1.IsMatch(input);
    ??? }

    ?? }
    ?? /* *******************************************************************
    ?? * 1、通過“:”來分割字符串看得到的字符串數組長度是否小于等于8
    ?? * 2、判斷輸入的IPV6字符串中是否有“::”。
    ?? * 3、如果沒有“::”采用 ^([/da-f]{1,4}:){7}[/da-f]{1,4}$ 來判斷
    ?? * 4、如果有“::” ,判斷"::"是否止出現一次
    ?? * 5、如果出現一次以上 返回false
    ?? * 6、^([/da-f]{1,4}:){0,5}::([/da-f]{1,4}:){0,5}[/da-f]{1,4}$
    ?? * ******************************************************************/
    ?? /// <summary>
    ?? /// 判斷字符串compare 在 input字符串中出現的次數
    ?? /// </summary>
    ?? /// <param name="input">源字符串</param>
    ?? /// <param name="compare">用于比較的字符串</param>
    ?? /// <returns>字符串compare 在 input字符串中出現的次數</returns>
    ?? private static int GetStringCount(string input, string compare)
    ?? {
    ??? int index = input.IndexOf(compare);
    ??? if(index != -1)
    ??? {
    ???? return 1 + GetStringCount(input.Substring(index + compare.Length),compare);
    ??? }
    ??? else
    ??? {
    ???? return 0;
    ??? }

    ?? }
    }
    }

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的C# Regex类详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。