C#中如何利用正则表达式判断字符
程序员文章站
2022-07-02 22:02:56
废话不多说了,下面代码给大家介绍下利用正则表达式判断字符的方法,具体代码如下所示:
using system;
using system.text.regula...
废话不多说了,下面代码给大家介绍下利用正则表达式判断字符的方法,具体代码如下所示:
using system; using system.text.regularexpressions; using system.net; namespace 正则表达式检测字符串 { class program { static void main(string[] args) { console.writeline("请输入字符串:"); string s = console.readline(); if (gf_isok.isexisthanzi(s)) { console.write("包含汉字"); } else { console.write("不包含汉字"); } console.readline(); } } //判断部分 public class gf_isok { /// <summary> /// 判读是否是ip地址 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool isipstr(string in_str) { ipaddress ip; return ipaddress.tryparse(in_str, out ip); } /// <summary> /// 判断是否是数字 /// </summary> /// <param name="strnumber"></param> /// <returns></returns> public static bool isnumber(string strnumber) { regex objnotnumberpattern = new regex("[^0-9.-]"); regex objtwodotpattern = new regex("[0-9]*[.][0-9]*[.][0-9]*"); regex objtwominuspattern = new regex("[0-9]*[-][0-9]*[-][0-9]*"); string strvalidrealpattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; string strvalidintegerpattern = "^([-]|[0-9])[0-9]*$"; regex objnumberpattern = new regex("(" + strvalidrealpattern + ")|(" + strvalidintegerpattern + ")"); return !objnotnumberpattern.ismatch(strnumber) && !objtwodotpattern.ismatch(strnumber) && !objtwominuspattern.ismatch(strnumber) && objnumberpattern.ismatch(strnumber); } /// <summary> /// 判断是否是日期字符串 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool isdatestr_yyyymmdd(string in_str) { if (in_str == "") return true; if (in_str.length != 8) return false; return isdatestr(in_str); } /// <summary> /// 判断是否是日期字符串 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool isdatestr(string in_str) { if (in_str == "") return true; if (in_str.length == 8) in_str = in_str.substring(0, 4) + "-" + in_str.substring(4, 2) + "-" + in_str.substring(6, 2); datetime dtdate; bool bvalid = true; try { dtdate = datetime.parse(in_str); } catch (formatexception) { // 如果解析方法失败则表示不是日期性数据 bvalid = false; } return bvalid; } /// <summary> /// 判断字符串中是否包含汉字,有返回true 否则为false /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool isexisthanzi(string str) { regex reg = new regex(@"[\u4e00-\u9fa5]");//正则表达式 if (reg.ismatch(str)) { return true; } else { return false; } } /// <summary> /// 字段串是否为null或为""(空) /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool isstrnullorempty(string str) { if (str == null || str.trim() == string.empty) return true; return false; } /// <summary> /// 返回文件是否存在 /// </summary> /// <param name="filename">文件名</param> /// <returns>是否存在</returns> public static bool isfileexists(string filename) { return system.io.file.exists(filename); } /// <summary> /// 检测是否符合email格式 /// </summary> /// <param name="stremail">要判断的email字符串</param> /// <returns>判断结果</returns> public static bool isvalidemail(string stremail) { return regex.ismatch(stremail, @"^[\w\.]+([-]\w+)*@[a-za-z0-9-_]+[\.][a-za-z0-9-_]"); } public static bool isvaliddoemail(string stremail) { return regex.ismatch(stremail, @"^@(( [0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−za−z]2,4|[0−9]1,3)( ?)$"); } /// <summary> /// 检测是否是正确的url /// </summary> /// <param name="strurl">要验证的url</param> /// <returns>判断结果</returns> public static bool isurl(string strurl) { return regex.ismatch(strurl, @"^(http|https)\://([a-za-z0-9\.\-]+(\:[a-za-z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-za-z0-9\-]+\.)*[a-za-z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-za-z]{1,10}))(\:[0-9]+)*(/($|[a-za-z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); } /// <summary> /// 判断是否为base64字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool isbase64string(string str) { //a-z, a-z, 0-9, +, /, = return regex.ismatch(str, @"[a-za-z0-9\+\/\=]"); } /// <summary> /// 检测是否有sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool issafesqlstring(string str) { return !regex.ismatch(str, @"[-|;|,|\/||| | |\}|\{|%|@|\*|!|\']"); } } }
以上所述是小编给大家介绍的c#中如何利用正则表达式判断字符,希望对大家有所帮助
上一篇: 详解C#批量插入数据到Sqlserver中的四种方式
下一篇: Oracle安装及相关配置