Home >Backend Development >C#.Net Tutorial >C# regular expression open source tool
Let me give you some background first. Regular expressions are often used in my work recently, and I personally feel that regular expressions are useless. Don’t use them. Some functions will be very troublesome to implement. Use it, it doesn’t mean that you often use it at work, it’s just that sometimes you need to use it. But as long as regular expressions are not used for a period of time, they will be forgotten, or even completely forgotten. In order to solve this useless problem to a certain extent, I came up with this blog and the idea of this open source regular expression verification tool that I plan to write. There is actually a lot of information about regular rules online. The reason why I wrote this blog is just to record some regular rules that may be used in current or future work, and then share them. At the same time, I hope to do something for .net open source.
0. Written in front
I remember that the last time I used regular expressions seriously was three years ago. Although I also used them sporadically during the period, I have basically forgotten them. It’s almost done, so if there is anything wrong in this blog, you are welcome and thank you for your correction! Here I just wrote down some regular matches that are commonly used in my personal work. If there are garden friends who are willing to contribute to the coding of regular open source tools, I will be very welcome.
Recently, there have been more and more "menstrual patches" in the garden, and I was wondering, are these people just too idle? If you have time to criticize this language and criticize that environment, you might as well open your arms and embrace the .net open source trend. With the arrival of .net open source, the spring of .net is also coming. We should extend our hands to do something for .net open source and make the .net community more and more open source. Of course this is off topic, those menstrual patches are none of my business.
v1. Source code address
https://github.com/toutouge/TouTou.RegexToolBack to top
v2.Start of text
2.1. : Universal matching
C# Universal matching rules, you can pass in the source string and the corresponding regular expression when calling
/// <summary> /// 检测字符串中是否包含符合正则的子集 /// </summary> /// <param name="source">源字符串</param> /// <param name="reg">正则, e.g. \d+</param> /// <returns>true:包含,反之不包含</returns> publicbool CheckContainsByReg(string source, string reg) { returnRegex.Match(source, reg).Success; } /// <summary> /// 检测整个字符串是否能匹配正则,而不是包含 /// </summary> /// <param name="source">源字符串</param> /// <param name="reg">正则, e.g. ^\d+$</param> /// <returns>true:匹配,反之不匹配</returns> publicbool CheckStringByReg(string source, string reg) { Regex rg = newRegex(reg, RegexOptions.IgnoreCase); return rg.IsMatch(source); } /// <summary> /// 从指定字符串中过滤出第一个符合正则匹配的子集 /// </summary> /// <param name="source">源字符串</param> /// <param name="reg">正则, e.g. \d+</param> /// <returns>源字符串的第一个匹配的子集</returns> publicstring GetFirstStringByReg(string source, string reg) { returnRegex.Match(source, reg).Groups[0].Value; } /// <summary> /// 从指定字符串中过滤出所有符合正则匹配的子集 /// </summary> /// <param name="source">源字符串</param> /// <param name="reg">正则, e.g. \d+</param> /// <returns>true:匹配,反之不匹配</returns> publicList<string> GetStringByReg(string source, string reg) { var regex = Regex.Matches(source, reg); List<string> list =newList<string>(); foreach (Match item in regex) { list.Add(item.Value); } return list; }
2.2.: Number matching
C# regular expression Match numbers according to various needs
/// <summary> /// 从指定字符串中过滤出第一个数字 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串的第一个数字</returns> publicstring GetFirstNumberByString(string source) { returnRegex. Match(source, @"\d+").Groups[0].Value; } /// <summary> /// 从指定字符串中过滤出最后一个数字 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串的最后一个数字</returns> publicstring GetLastNumberByString(string source) { var reg = Regex.Matches(source, @"\d+"); return reg[reg.Count -1].Value; } /// <summary> /// 从指定字符串中过滤出所有数字 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串的所有数字</returns> publicList<string> GetAllNumberByString( string source) { var reg = Regex.Matches(source, @"\d+"); List<string> list =newList<string>(); foreach (Match item in reg) { list.Add(item.Value); } return list; } /// <summary> /// 检车源字符串中是否包含数字 /// </summary> /// <param name="source">源字符串</param> /// <returns>true:源字符串包含数字;false:源字符串不包含数字</returns> publicbool CheckNumberByString(string source) { returnRegex. Match(source, @"\d").Success; } /// <summary> /// 判断字符串是否全部是数字且长度等于指定长度 /// </summary> /// <param name="source">源字符串</param> /// <param name="length">指定长度</param> /// <returns>返回值</returns> publicbool CheckLengthByString(string source, int length) { Regex rg = newRegex(@"^\d{" + length +"}$"); return rg.IsMatch(source); }
2.3.: Regular string interception
C#Intercept the string between the beginning according to the given starting character
/// <summary> /// 截取字符串中开始和结束字符串中间的字符串 /// </summary> /// <param name="source">源字符串</param> /// <param name="startStr">开始字符串</param> /// <param name="endStr">结束字符串</param> /// <returns>中间字符串</returns> publicstring Substring(string source, string startStr, string endStr) { Regex rg = newRegex("(?<=(" + startStr +"))[.\\s\\S]*?(?=(" + endStr +"))", RegexOptions.Multiline |RegexOptions.Singleline); return rg.Match(source).Value; }
2.4.: 邮箱匹配
C#正则表达式匹配邮箱
/// <summary> /// 匹配邮箱是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是邮箱反之不是邮箱</returns> publicbool CheckEmailByString(string source) { Regex rg = newRegex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$", RegexOptions.IgnoreCase); return rg.IsMatch(source); }
2.5.: URL匹配
C#正则表达式匹配URL
/// <summary> /// 匹配URL是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是URL反之不是URL</returns> publicbool CheckURLByString(string source) { Regex rg = newRegex(@"^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)? (((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\. (\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]) ([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])* ([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]) ([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~| [\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])| (%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)? (\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~| [\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$" , RegexOptions.IgnoreCase); return rg.IsMatch(source); }
2.6.: 日期匹配
C#正则表达式匹配日期
/// <summary> /// 匹配日期是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是日期反之不是日期</returns> publicbool CheckDateByString(string source) { Regex rg = newRegex(@"^(\d{4}[\/\-](0?[1-9]|1[0-2])[\/\-]((0?[1-9])|((1|2)[0-9])|30|31))|((0?[1-9]|1[0-2])[\/\-]((0?[1-9])| ((1|2)[0-9])|30|31)[\/\-]\d{4})$"); return rg.IsMatch(source); } /// <summary> /// 从字符串中获取第一个日期 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串中的第一个日期</returns> publicstring GetFirstDateByString(string source) { returnRegex.Match(source,@"(\d{4}[\/\-](0?[1-9]|1[0-2])[\/\-]((0?[1-9])|((1|2)[0-9])|30|31))|((0?[1-9]|1[0-2])[\/\-]((0?[1-9])| ((1|2)[0-9])|30|31)[\/\-]\d{4})").Groups[0].Value; } /// <summary> /// 从字符串中获取所有的日期 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串中的所有日期</returns> publicList<string> GetAllDateByString( string source) { var all = Regex.Matches(source, @"(\d{4}[\/\-](0?[1-9]|1[0-2])[\/\-]((0?[1-9])|((1|2)[0-9])|30|31))|((0?[1-9]|1[0-2]) [\/\-]((0?[1-9])|((1|2)[0-9])|30|31)[\/\-]\d{4})"); List<string> list =newList<string>(); foreach (Match item in all) { list.Add(item.Value); } return list; }
2.7.: 密码匹配
C#正则表达式匹配密码
/// <summary> /// 检测密码复杂度是否达标:密码中必须包含字母、数字、特称字符,至少8个字符,最多16个字符。 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>密码复杂度是否达标true是达标反之不达标</returns> publicbool CheckPasswordByString(string source) { Regex rg = newRegex(@"^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$"); return rg.IsMatch(source); }
2.8.: 邮编匹配
C#正则表达式匹配邮编
/// <summary> /// 匹配邮编是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>邮编合法返回true,反之不合法</returns> publicbool CheckPostcodeByString(string source) { Regex rg = newRegex(@"^\d{6}$"); return rg.IsMatch(source); }
2.9.: 电话号码
C#正则表达式匹配电话
/// <summary> /// 匹配电话号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>电话号码合法返回true,反之不合法</returns> publicbool CheckTelephoneByString(string source) { Regex rg = newRegex(@"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"); return rg.IsMatch(source); } /// <summary> /// 从字符串中获取电话号码 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串中电话号码</returns> publicstring GetTelephoneByString(string source) { returnRegex. Match(source, @"(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}").Groups[0].Value; }
2.10.: 手机号码
C#正则表达式匹配手机号码
/// <summary> /// 匹配手机号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>手机号码合法返回true,反之不合法</returns> publicbool CheckMobilephoneByString(string source) { Regex rg = newRegex(@"^[1]+[3,5,7]+\d{9}$"); return rg.IsMatch(source); } /// <summary> /// 从字符串中获取手机号码 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串中手机号码</returns> publicstring GetMobilephoneByString(string source) { returnRegex. Match(source, @"[1]+[3,5,7]+\d{9}").Groups[0].Value; }
2.11.: 身份证匹配
C#正则表达式匹配身份证号码
/// <summary> /// 匹配身份证号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>身份证号码合法返回true,反之不合法</returns> publicbool CheckIDCardByString(string source) { Regex rg = newRegex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); return rg.IsMatch(source); } /// <summary> /// 从字符串中获取身份证号码 /// </summary> /// <param name="source">源字符串</param> /// <returns>源字符串中身份证号码</returns> publicstring GetIDCardByString(string source) { returnRegex. Match(source, @"(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))").Groups[0].Value; }
v3.博客总结
关于C#正则表达式开源工具暂时就只积累了这么多。因为目前实际工作中遇到的也就这些,关于这个C#正则表达式开源工具现在只是一个雏形,先把这一分不分享出来,后续得会持续更新C#正则表达式开源工具。希望在以后这个C#正则表达式工具会越来越strong,更希望能得到园友们的support.
以上就是C#正则表达式开源工具的内容,更多相关内容请关注PHP中文网(www.php.cn)!