SoFunction
Updated on 2025-03-08

Example of commonly used regular verification functions in C#

This article describes the commonly used regular verification functions of C#. Share it for your reference, as follows:

1. IP address verification

/// <summary>
/// IP address verification/// </summary>
public static bool CheckIp(string ip)
{
  bool result = false;
  Regex ipReg = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
  if ((ip))
  {
    result = true;
  }
  return result;
}

2. Price verification

/// <summary>
/// Price verification/// </summary>
/// <param name="priceStr"></param>
/// <returns></returns>
public bool CheckPrice(string priceStr)
{
  bool result = false;
  Regex regex = new Regex(@"^\d+(\.\d{1,2})?$", );
  Match match = (priceStr);
  if ()
  {
    result = true;
  }
  return result;
}

3. Positive integer verification

/// <summary>
/// Positive integer verification/// </summary>
public static bool CheckPositiveInteger(string numStr)
{
  bool result = false;
  Regex regex = new Regex(@"^[1-9]\d*$", );
  Match match = (numStr);
  if ()
  {
    result = true;
  }
  return result;
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools./regex/javascript

Regular expression online generation tool:
http://tools./regex/create_reg

For more information about C# related content, please check out the topic of this site:Summary of the usage of C# regular expressions》、《Summary of C# coding operation skills》、《Summary of XML file operation skills in C#》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.