SoFunction
Updated on 2025-03-07

How to use regular expressions to judge characters in C#

I won’t say much nonsense. The following code introduces you to the method of judging characters using regular expressions. The specific code is as follows:

using System;
using ;
using ;
namespace Regular expression detection string
{
 class Program
 {
  static void Main(string[] args)
  {
   ("Please enter the string:");
   string s = ();
   if (GF_IsOk.IsExistHanZi(s))
   {
    ("Including Chinese characters");
   }
   else
   {
    ("No Chinese characters");
   }
   ();
  }
 }
 //Judgement part public class GF_IsOk
 {
  /// <summary>
  /// Determine whether it is an IP address  /// </summary>
  /// <param name="in_str"></param>
  /// <returns></returns>
  public static bool IsIPStr(string in_str)
  {
   IPAddress ip;
   return (in_str, out ip);
  }
  /// <summary>
  /// Determine whether it is a number  /// </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 !(strNumber) &&
     !(strNumber) &&
     !(strNumber) &&
     (strNumber);
  }
  /// <summary>
  /// Determine whether it is a date string  /// </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>
  /// Determine whether it is a date string  /// </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 = (in_str);
   }
   catch (FormatException)
   {
    // If the parsing method fails, it means that it is not dated data    bValid = false;
   }
   return bValid;
  }
  /// <summary>
  /// Determine whether the string contains Chinese characters, return true if there is any. Otherwise, false  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>
  public static bool IsExistHanZi(string str)
  {
   Regex reg = new Regex(@"[\u4e00-\u9fa5]");// Regular expression   if ((str))
   {
    return true;
   }
   else
   {
    return false;
   }
  }

  /// <summary>
  /// Is the field string Null or "" (empty)  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>
  public static bool IsStrNullOrEmpty(string str)
  {
   if (str == null || () == )
    return true;
   return false;
  }
  /// <summary>
  /// Return whether the file exists  /// </summary>
  /// <param name="filename">File name</param>  /// <returns>Whether it exists</returns>  public static bool IsFileExists(string filename)
  {
   return (filename);
  }

  /// &lt;summary&gt;
  /// Check whether it conforms to the email format  /// &lt;/summary&gt;
  /// <param name="strEmail">email string to judge</param>  /// <returns>Judgement Results</returns>  public static bool IsValidEmail(string strEmail)
  {
   return (strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
  }
  public static bool IsValidDoEmail(string strEmail)
  {
   return (strEmail, @"^@((
[0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[0−9]1,3)(
?)$");
  }
  /// &lt;summary&gt;
  /// Check if the URL is correct  /// &lt;/summary&gt;
  /// <param name="strUrl">Url to be verified</param>  /// <returns>Judgement Results</returns>  public static bool IsURL(string strUrl)
  {
   return (strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((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\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
  }
 
  /// &lt;summary&gt;
  /// Determine whether it is a base64 string  /// &lt;/summary&gt;
  /// &lt;param name="str"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static bool IsBase64String(string str)
  {
   //A-Z, a-z, 0-9, +, /, =
   return (str, @"[A-Za-z0-9\+\/\=]");
  }
  /// &lt;summary&gt;
  /// Detect whether there are dangerous Sql characters  /// &lt;/summary&gt;
  /// <param name="str">To judge string</param>  /// <returns>Judgement Results</returns>  public static bool IsSafeSqlString(string str)
  {
   return !(str, @"[-|;|,|\/|||
|
|\}|\{|%|@|\*|!|\']");
  }
 }
}

The above is how to use regular expressions to judge characters in C# that the editor introduces to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!