当前位置:网站首页>Regular expression string
Regular expression string
2022-07-07 15:53:00 【Le_ Sam】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace tablegen2.common
{
#region Regular expression string
/// <summary>
/// Regular expression string
/// </summary>
public class StrRegex
{
#region Regular expressions replace strings
/// <summary>
/// Regular expressions replace strings
/// </summary>
/// <param name="inputString"> String content </param>
/// <param name="pattern"> Replace character </param>
/// <param name="replaceStr"> Replacement value </param>
/// <returns></returns>
public static string RegexReplace(string inputString, string pattern, string replaceStr)
{
try
{
return Regex.Replace(inputString, pattern, replaceStr);
}
catch (Exception e)
{
return e.Message;
}
}
#endregion
#region Determine whether the string is a positive integer
/// <summary>
/// Determine whether the string is a positive integer
/// </summary>
/// <param name="objString"> String to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool IsInt(String objString)
{
Regex myReg = new Regex(@"^\d+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the input string is all in English ( Case insensitive )
/// <summary>
/// Judge whether the input string is all in English ( Case insensitive )
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isEnglishString(String objString)
{
Regex myReg = new Regex(@"^[a-zA-Z]+$");
return myReg.IsMatch(objString);
}
#endregion
/// <summary>
/// Returns the number in a string
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunNumber(string objString)
{
return Regex.Match(objString, "[0-9]+").Value.ToString();
}
/// <summary>
/// Returns the left character in the string
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunLeftString(string objString)
{
return Regex.Match(objString, "[%*/+ -.A-Za-z]+").Value.ToString();
}
/// <summary>
/// Returns the right character in the string
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunRightString(string objString)
{
return Regex.Match(objString, "[%*/+ -.A-Za-z]+$").Value.ToString();
}
/// <summary>
/// Returns the characters in a string
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static string RunString(string objString)
{
return Regex.Match(objString, "[A-Za-z]+").Value.ToString();
}
#region Judge whether the input string is Chinese
/// <summary>
/// Judge whether the input string is Chinese
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isChinese(String objString)
{
Regex myReg = new Regex(@"^[\u4e00-\u9fa5]+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the input string is in English and numbers ( English is not case sensitive )
/// <summary>
/// Judge whether the input string is in English and numbers ( English is not case sensitive )
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isEngNum(String objString)
{
Regex myReg = new Regex(@"^[*/+-a-zA-Z0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the input string is in English A-D And numbers ( English is limited to A-D English is not case sensitive )
/// <summary>
/// Judge whether the input string is in English A-D And numbers ( English is limited to A-D English is not case sensitive )
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isEngNumMax(String objString)
{
Regex myReg = new Regex(@"^[a-dA-D0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether it is a combination of English and numbers
/// <summary>
/// Judge whether it is a combination of English and numbers
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static bool InEngNum(string objString)
{
//Regex myReg = new Regex(@"^(?![0-9]+$)[a-zA-Z0-9]{1,25}$");
//return myReg.IsMatch(objString);"^[a-zA-Z]\w{5,17}$"
return Regex.IsMatch(objString, @"^[*/+-a-zA-Z0-9]{1,20}$");
}
#endregion
#region Judge whether the input string is in English , Numbers , chinese ( English is not case sensitive )
/// <summary>
/// Judge whether the input string is in English , Numbers , chinese ( English is not case sensitive )
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isChineseEngNum(String objString)
{
Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the input string is decimal
/// <summary>
/// Judge whether the input string is decimal
/// </summary>
/// <param name="objString"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool isFloat(String objString)
{
Regex myReg = new Regex(@"^[0-9]+[.][0-9]+|[0-9]+$");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the date format is valid
/// <summary>
/// Judge whether the date format is valid
/// </summary>
/// <param name="objString"></param>
/// <returns></returns>
public static bool IsDate(String objString)
{
Regex myReg = new Regex(@"\b(?<year>\d{2,4})-(?<month>\d{1,2})-(?<day>\d{1,2})\b");
return myReg.IsMatch(objString);
}
#endregion
#region Judge whether the string conforms to this regular expression
/// <summary>
/// Judge whether the string conforms to this regular expression
/// </summary>
/// <param name="str"> The string to match </param>
/// <param name="regString"> Regular string ( Such as :^[1-9]{1}$)</param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool IsFitStrings(String str, String regString)
{
Regex objPattern = new Regex(regString);
bool returnValue = objPattern.IsMatch(str);
return returnValue;
}
#endregion
#region Judge whether the string is a mobile number or a small smart number
/// <summary>
/// Judge whether the string is a mobile number or a small smart number
/// </summary>
/// <param name="telNumber"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool IsMobile(string telNumber)
{
if (telNumber == "")
return false;
Regex myReg = new Regex(@"^((\d{11,12})|(\d{7}))$");
return myReg.IsMatch(telNumber);
}
#endregion
#region Determine if the string is Email Address
/// <summary>
/// Determine if the string is Email Address
/// </summary>
/// <param name="email"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool IsEmail(string email)
{
if (email == "")
{
return false;
}
Regex myReg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
return myReg.IsMatch(email);
}
#endregion
#region Judge whether the string is a landline ( Such as xxx-xxxxxxx-xxx)
/// <summary>
/// Judge whether the string is a landline ( Such as xxx-xxxxxxx-xxx)
/// </summary>
/// <param name="tel"> The string to match </param>
/// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
public static bool IsTel(string tel)
{
if (tel == "")
return false;
Regex myReg = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}(-\d{1,5})?$");
return myReg.IsMatch(tel);
}
#endregion
#region Determine whether it is a postal code
/// <summary>
/// Determine whether it is a postal code
/// </summary>
/// <param name="Zip"></param>
/// <returns></returns>
public static bool IsValidZip(string Zip)
{
return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$");
}
#endregion
#region Determine whether it is a valid ID number
/// <summary>
/// Determine whether it is a valid ID number
/// </summary>
/// <param name="IdCard"></param>
/// <returns></returns>
public static bool IsIdCard(string IdCard)
{
return Regex.IsMatch(IdCard, @"^\d{15}|\d{18}$");
}
#endregion
#region Returns the split string
/// <summary>
/// Returns the split string
/// </summary>
/// <param name="Str"> The set of strings to split </param>
/// <param name="spliststr"> Specify split character </param>
/// <returns></returns>
public static string FindStr(string Str, string spliststr)
{
string[] str2 = System.Text.RegularExpressions.Regex.Split(Str, @"[" + spliststr + "]+");
foreach (string i in str2)
{
return i.ToString();
}
return "";
}
#endregion
}
#endregion
}
边栏推荐
- 尤雨溪,来了!
- C4D learning notes 3- animation - animation rendering process case
- [quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
- Shader basic UV operations, translation, rotation, scaling
- 【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
- How to understand that binary complement represents negative numbers
- What is Base64?
- Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
- [original] all management without assessment is nonsense!
- 2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
猜你喜欢
Numpy -- epidemic data analysis case
15. Using the text editing tool VIM
尤雨溪,来了!
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
HW primary flow monitoring, what should we do
保证接口数据安全的10种方案
Ue4/ue5 multi thread development attachment plug-in download address
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
TS as a general cache method
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
随机推荐
HPDC smart base Talent Development Summit essay
Whether runnable can be interrupted
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Webgl texture
LeetCode1_ Sum of two numbers
Iterator and for of.. loop
numpy--数据清洗
尤雨溪,来了!
A link opens the applet code. After compilation, it is easy to understand
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
numpy--疫情数据分析案例
Migration and reprint
Annexb and avcc are two methods of data segmentation in decoding
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
Monthly observation of internet medical field in May 2022
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)