c#常用正則表達式

public class RegexUtil
{private RegexUtil() { }private static RegexUtil instance = null;/// <summary>/// 靜態實例化單體模式/// 保證應用程序操作某一全局對象,讓其保持一致而產生的對象/// </summary>/// <returns></returns>public static RegexUtil GetInstance(){if (RegexUtil.instance == null){RegexUtil.instance = new RegexUtil();}return RegexUtil.instance;}/// <summary>/// 判斷輸入的字符串只包含漢字/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsChineseCh(string input){return IsMatch(@"^[\u4e00-\u9fa5]+$", 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}$";return IsMatch(pattern, input);            }/// <summary>/// 判斷輸入的字符串是否是一個合法的手機號/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsMobilePhone(string input){return IsMatch(@"^13\\d{9}$", input);}/// <summary>/// 判斷輸入的字符串只包含數字/// 可以匹配整數和浮點數/// ^-?\d+$|^(-?\d+)(\.\d+)?$/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsNumber(string input){string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";return IsMatch(pattern, input);            }/// <summary>/// 匹配非負整數/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsNotNagtive(string input){return IsMatch(@"^\d+$", input);}/// <summary>/// 匹配正整數/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsUint(string input){return IsMatch(@"^[0-9]*[1-9][0-9]*$", input);            }/// <summary>/// 判斷輸入的字符串字包含英文字母/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsEnglisCh(string input){return IsMatch(@"^[A-Za-z]+$", 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})(\]?)$";return IsMatch(pattern, input);            }/// <summary>/// 判斷輸入的字符串是否只包含數字和英文字母/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsNumAndEnCh(string input){return IsMatch(@"^[A-Za-z0-9]+$", input);}/// <summary>/// 判斷輸入的字符串是否是一個超鏈接/// </summary>/// <param name="input"></param>/// <returns></returns>public static bool IsURL(string input){string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";return IsMatch(pattern, input);            }/// <summary>/// 判斷輸入的字符串是否是表示一個IP地址/// </summary>/// <param name="input">被比較的字符串</param>/// <returns>是IP地址則為True</returns>public static bool IsIPv4(string input){string[] IPs = input.Split('.');for (int i = 0; i < IPs.Length; i++){if (!IsMatch(@"^\d+$",IPs[i])){return false;}if (Convert.ToUInt16(IPs[i]) > 255){return false;}}return true;}/// <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 = RegexUtil.GetStringCount(input, "::");if (count > 1){return false;}else if (count == 0){pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";return IsMatch(pattern, input);}else{pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";return IsMatch(pattern, input);}}#region 正則的通用方法/// <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){if (input == null || input == "") return false;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);}/* ******************************************************************** 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;}}/// <summary>/// 根據結構體RegularFormula,返回具體正則表達式/// </summary>/// <param name="RegulatFormula"></param>/// <returns></returns>private static string GetRegularFormula(string RegulatFormula){string str = "";if (RegulatFormula.ToUpper().IndexOf("RegularFormula".ToUpper()) > -1){RegularFormula formula = new RegularFormula();Type type = typeof(RegularFormula);foreach (System.Reflection.FieldInfo info in type.GetFields()){if (("RegularFormula." + info.Name).ToUpper() == RegulatFormula.ToUpper()){str = info.GetValue(formula).ToString();}}return str;}return RegulatFormula;}#endregion
}

?【@網絡】

轉載于:https://www.cnblogs.com/im404/articles/4320495.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273366.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273366.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273366.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Http協議(4)—HTTP認證機制

一、認證1.HTTP質詢/響應認證框架服務器收到一條請求并沒有按照請求執行動作,而是以一個認證質詢執行響應,要求用戶提供一個保密信息說明他是誰,當用戶再次發送請求時要附上保密證書,如果證書匹配則執行請求,否則返回一條錯誤信息2.認證協議與首部官方的兩個認證協議:基本認證、…

C#加密解密DES字符串轉

using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO;namespace Component {public class Security{public Security(){ }//默認密鑰向量private static byte[] Keys { 0x12, 0x34, 0x56, 0x78, 0x90, …

Http協議(6)—安全HTTP

一、保護HTTP的安全1.功能:.服務器認證:客戶端知道它是在與真正的服務器進行通信.客戶端認證:服務器知道它是在與真正的客戶端進行通信.完整性:服務器與客戶端的數據不會被修改.加密:客戶端與服務器的對話是私密的,不會被竊聽.效率:運行足夠快的算法.普適性:所有客戶端和服務器…

restful處理

重寫/覆蓋 HTTP 方法 一些HTTP客戶端僅能處理簡單的的GET和POST請求&#xff0c;為照顧這些功能有限的客戶端&#xff0c;API需要一種方式來重寫HTTP方法. 盡管沒有一些硬性標準來做這事&#xff0c;但流行的慣例是接受一種叫 X-HTTP的請求頭&#xff0c;重寫是用一個字符串值…

Http協議(7)—Http緩存

一、冗余的數據傳輸有些客戶端訪問服務器頁面時,服務器會多次響應同一個頁面的副本給客戶端&#xff0c;這會產生冗余數據&#xff0c;故使用緩存就可以保留第一條相應的副本&#xff0c;以后就響應緩存的數據二、帶寬瓶頸在需要下載大型文件時,如果在局域網中放入該文件的一個…

Apache JMeter--網站自動測試與性能測評

Apache JMeter--網站自動測試與性能測評2013-02-28 15:48:05標簽&#xff1a;JmeterFrom:http://bdql.iteye.com/blog/291987 出于學習熱情&#xff0c;翻譯總結Emily H. Halili的《Apache JMeter》一書的部分內容。 JMeter的簡介 可以肯定的是&#xff0c;JMeter至少符合以下幾…

Linux 重命名文件

inux下重命名文件或文件夾的命令mv既可以重命名&#xff0c;又可以移動文件或文件夾. 例子&#xff1a;將目錄A重命名為B mv A B 例子&#xff1a;將/a目錄移動到/b下&#xff0c;并重命名為c mv /a /b/c 其實在文本模式中要重命名文件或目錄的話也是很簡單的&#xff0c;我們只…

苦逼的.net程序員, 轉行高富帥iOS移動開發

先知先覺,后知后覺 **- 在做了兩三年.net開發后, 還是感覺.net不是那么牛逼, 許多給我一起搞.net的同學, 不是去做了android, 就是去做了iOS, 或者java; 這讓我對.net的前景有了一些動搖, 在三思考之后,還是決定放棄.net ,理由很簡單,就是工資有點低; 由于藍鷗iOS培訓機構,一…

C# DataTable的詳細使用方法

在項目中經經常使用到DataTable,假設DataTable使用得當&#xff0c;不僅能使程序簡潔有用&#xff0c;并且可以提高性能&#xff0c;達到事半功倍的效果&#xff0c;現對DataTable的使用技巧進行一下總結。 一、DataTable簡單介紹 (1)構造函數 DataTable() 不…

mysql設置環境變量

-- 設置或修改系統日志有效期SET GLOBAL expire_logs_days8;SHOW VARIABLES LIKE %expire_logs_days%;-- 設置或修改系統最大連接數SET GLOBAL max_connections 2648;SHOW VARIABLES LIKE %max_connections%;-- 修改MYSQL自動編號步長SHOW VARIABLES LIKE %auto_increment%;SE…

CentOS7 編譯安裝LVS 互為主備 (實測 筆記 Centos 7.0 + ipvsadm 1.27 + keepalived 1.2.15 )

環境&#xff1a; 系統硬件&#xff1a;vmware vsphere (CPU&#xff1a;2*4核&#xff0c;內存2G&#xff0c;雙網卡) LVS服務器&#xff08;兩臺&#xff09;&#xff1a; 系統&#xff1a;Centos7.0 64位&#xff08;LVSkeepalived&#xff09; LvsMaster:192.168.1.21 (主…

shell 執行mysql語句

<pre name"code" class"plain">#變量定義 sqlname"test.sql" dir"/sdb2/backup/mysql_db_backup/backup/databases" host"127.0.0.1" user"root" passwd"root" dbname"test" #導…

hdu3081 Marriage Match II(最大流)

轉載請注明出處&#xff1a; http://www.cnblogs.com/fraud/ ——by fraud Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2410 Accepted Submission(s): 820 Problem Descriptio…

CentOS6安裝tomcat6

首先我們要下載一個tomcat的安裝包 http://ftp.riken.jp/net/apache/ wget http://ftp.riken.jp/net/apache/tomcat/tomcat-6/v6.0.41/src/apache-tomcat-6.0.41.tar.gz 下載好后解壓到一個以目錄&#xff0c;我的是放在了/usr/apache-tomcat-6.0.41 tar –zxvf apache-t…

修復 XE7 , XE8 Frame 內 PopupMenu 快捷鍵失效問題

問題&#xff1a;將 Frame 含 PopupMenu 放置 Form 后&#xff0c;在 Frame 里的 PopupMenu 失效&#xff0c;無法按快捷鍵。 適用&#xff1a;(XE7 update 1 / XE8) for Windows 平臺 修正方法&#xff1a; 請將源碼 FMX.Forms.pas 復制到自己的工程目錄里&#xff0c;再進行修…

Vmware Centos中安裝vmtools工具

在Vmware安裝虛擬機是很好玩的&#xff0c;可是有時候在虛擬機與本地主機之間相互傳遞文件時卻是一件比較麻煩的事情&#xff0c;這時候我們安裝一個vmtools的工具這樣我們就可以隨意的在虛擬機與主機之間相互拖拽文件&#xff0c;下面我們就來說說如何安裝vmtools 點擊虛擬機會…

關于Dapper - 能否不創建定義表對應類使用

1.是可以的&#xff0c;而且支持的很棒 1 /*2 lcg3 * 1.看看能不能用4 * 2.怎么用 - 引哪個文件即可&#xff1f;5 */6 7 //數據庫連接參數8 private const string strConn "Data SourceAlen;Initial Catal…

動態規劃 背包九講的實現。

最近在學習動態規劃&#xff0c;會了不少基礎的之后就開始挑戰比較困難的背包問題了&#xff0c;我這里自己寫了每一講的問題&#xff0c;解析&#xff0c;代碼&#xff0c;注釋。如果dp還沒入門的孩紙就去看看我的另一篇文章http://www.cnblogs.com/luyi14/p/4344946.html …

Linux中查看負載

行車過橋 一只單核的處理器可以形象得比喻成一條單車道。設想下&#xff0c;你現在需要收取這條道路的過橋 費 — 忙于處理那些將要過橋的車輛。你首先當然需要了解些信息&#xff0c;例如車輛的載重、以及 還有多少車輛正在等待過橋。如果前面沒有車輛在等待&#xff0c;那么你…

flask小demo-數據查詢

mysqlconn-flask.py 1 # -*- coding: utf-8 -*-2 #codingutf-83 4 import os5 import mysql.connector6 from flask import Flask, request, render_template7 8 app Flask(__name__)9 10 def db(): 11 # 注意把password設為你的root口令: 12 conn mysql.connect…