Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

Regular expressions are also called regular expressions. Regular Expression (often abbreviated to regex, regexp, or RE in code) is a term used in computer science. Regular expressions are often used to retrieve and replace text that conforms to a pattern (rule).

Many programming languages support string manipulation using regular expressions. For example, there is a powerful regular expression engine built into Perl. The concept of regular expressions was first popularized by Unix tools such as sed and grep. Regular expressions are usually abbreviated to “regex”. There are regexp and regex in the singular and regexps, regexes, and regexen in the plural.

Unity uses regular expressions

Match positive integers:

using System.Text.RegularExpressions;
using UnityEngine;

public class Regex_Test : MonoBehaviour
{
    void Start()
    {
        string temp = "123";
        Debug.Log(IsNumber(temp));
    }

    ///<summary> 
    ///Match positive integers
    ///</summary> 
    ///<param name="strInput"></param>
    ///<returns></returns>
    public bool IsNumber(string strInput)
    {
        Regex reg = new Regex("^ [0-9] * [1-9] [0-9] * $");
        if (reg.IsMatch(strInput))
        {
            return true;
        }
        else
        {
            return false; }}}Copy the code

Results:

Match uppercase letters

using System.Text.RegularExpressions;
using UnityEngine;

public class Regex_Test : MonoBehaviour
{
    void Start()
    {
        string temp = "ABC";
        Debug.Log(IsNumber(temp));
    }
    ///<summary>
    ///Matches a string of 26 uppercase English letters
    ///</summary>
    ///<param name="strInput"></param>
    ///<returns></returns>
    public bool IsCapital(string strInput)
    {
        Regex reg = new Regex("^[A-Z]+$");
        if (reg.IsMatch(strInput))
        {
            return true;
        }
        else
        {
            return false; }}}Copy the code

Results:

Third, the Regex classes

The Regex class is used to represent a regular expression.

The following table lists some common methods in the Regex class:

The serial number methods describe
1 public bool IsMatch( string input ) Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string.
2 public bool IsMatch( string input, int startat ) Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, starting at the specified starting position in the string.
3 public static bool IsMatch( string input, string pattern ) Indicates whether the specified regular expression finds a match in the specified input string.
4 public MatchCollection Matches( string input ) Searches for all matches of the regular expression in the specified input string.
5 public string Replace( string input, string replacement ) Replaces all matched strings that match the regular expression pattern with the specified replacement string in the specified input string.
6 public string[] Split( string input ) Splits the input string into an array of substrings according to the position defined by the regular expression pattern specified in the Regex constructor.

For a complete list of properties for the Regex class, see Microsoft’s C# documentation.

4. Regular expressions

^ -? \d+$// Match integers (including positive and negative integers)^ (-? \d+)(\.\d+)? $// Match floating point numbers (including positive and negative floating points)^[A-Za-z]+$// Match 26 English letters (including case and case)^[A-Z]+$// Match with 26 English letters (uppercase)^[a-z]+$// Match with 26 English letters (lowercase)
^[A-Za-z09 -] + $// Matches a string of numbers and 26 letters^\w+$// Matches a string of numbers, 26 letters, or underscores
\S{6,} 				// No more than six digits can be empty
[^\x00-\xff] 		// Match double-byte characters (including Chinese characters)
\d+\.\d+\.\d+\.\d+	// Matches the IP address
Copy the code
The serial number code describe
1 ^\d+$ Match non-negative integers (positive integers + 0)
2 ^ [0-9] * [1-9] [0-9] * $ Match positive integers
3 ^((-\d+)|(0+))$ Match non-positive integers (negative integers + 0)
4 ^ – [0-9] * [1-9] [0-9] * $ Matching negative integers
5 ^ -? \d+$ Match the integer
6 ^\d+(\.\d+)? $ Matches non-negative floating-point numbers (positive floating-point + 0)
7 ^ (([0-9] + \. [0-9][1-9] [0-9]) | ([0-9][1-9] [0-9][0-9] +) | ([0-9][1-9] [0-9])) $ Matches a positive floating point number
8 ^((-\d+(.\d+)?) | ((. 0 0 + +)?) ) $ Matches non-positive floating-point numbers (negative floating-point + 0)
9 ^ (- (([0-9] +. [0-9][1-9] [0-9]) | ([0-9][1-9] [0-9][0-9] +) | ([0-9][1-9] [0-9]))) $ Matches a negative floating point number
10 ^ (-? \d+)(.\d+)? $ Matching floating point
11 ^[A-Za-z]+$ Matches a string consisting of 26 English letters
12 ^[A-Z]+$ Matches a string of 26 uppercase English letters
13 ^[a-z]+$ Matches a string consisting of 26 lowercase letters
14 ^[A-Za-z0-9]+$ Matches a string of numbers and 26 letters
15 ^\w+$ Matches a string consisting of digits, 26 letters, or underscores
16 ^[\w-]+(.[\w-]+)*@[\w-]+(.[\w-]+)+$ Matching email address
17 ^ [a zA – z] + : / / match (\ w + (\ w +))(.(\w+(-\w+)))(? \S)? $ Match the url
18 [\u4e00-\u9fa5] A regular expression that matches Chinese characters
19 [^\x00-\xff] Matching double-byte characters (including Chinese characters)
20 String.prototype.len=function(){return this.replace([^\x00-\xff]/g,”aa”).length; } Application: Calculate the length of a string (a two-byte character length 2, ASCII character 1)
21 \n[\s| ]*\r Regular expressions that match empty lines
22 / < > (. *).< 1 > / \ | < (.) / > A regular expression that matches an HTML tag
23 (^\s*)|(\s*$) A regular expression that matches leading and trailing Spaces

Five, the instance

Example 1. Match words beginning with ‘m’ and ending with ‘e’

using System.Text.RegularExpressions;
using UnityEngine;

public class Regex_Test : MonoBehaviour
{
    void Start()
    {
        string temp = "make maze and manage to measure it";
        MatchStr(temp);
    }

    public void MatchStr(string str)
    {
        Regex reg = new Regex(@"\bm\S*e\b");
        MatchCollection mat = reg.Matches(str);
        foreach (Match item inmat) { Debug.Log(item); }}}Copy the code

Example 2. Replace the extra Spaces

using System.Text.RegularExpressions;
using UnityEngine;

public class Regex_Test : MonoBehaviour
{
    void Start()
    {
        string temp = "Hello World";
        MatchStr(temp);
    }


    public void MatchStr(string str)
    {
        Regex reg = new Regex("\\s+");
        Debug.Log(reg.Replace(str, "")); }}Copy the code

Find the number in the string and return it

using System.Text.RegularExpressions;
using UnityEngine;

public class Regex_Test : MonoBehaviour
{
    void Start()
    {
        string temp = "Hello 123 is not good.";
        MatchStr(temp);
    }


    public int MatchStr(string str)
    {
        Regex r = new Regex(@"\d+", RegexOptions.IgnoreCase);
        Match m = r.Match(strNumber);
        int number = int.Parse(m.Value);
        returnnumber; }}Copy the code