首先该方法属于C#,是C#提供的,不是unity提供的,但我使用这个是在unity中使用的,故此我将其归入unity笔记。
该方式可用于注册、登录等等需要用户输入的场景,用以判断字符串是否满足正则表达式的要求
首先介绍该API
//引入命名空间
using System.Text.RegularExpressions;
//如下API
public bool IsMatch(string input);
public bool IsMatch(string input, int startat);
public static bool IsMatch(string input, string pattern);
public static bool IsMatch(string input, string pattern, RegexOptions options);
public static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
这里开始介绍,大家可以看看官方的文档Regex.IsMatch 方法 (System.Text.RegularExpressions) | Microsoft Learn
以下是我的个人理解
首先是非静态的方法,需要配合Regex变量一起使用
//非静态
/*1*/
public bool IsMatch(string input);
/*
input是否满足自定义的Regex变量的正则表达式
满足返回true,否则false
*/
//使用
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
//需要检测的字符串
string[] partNumbers= {
"1298-673-4192",
"A08Z-931-468A",
"_A90-123-129X",
"12345-KKA-1230",
"0919-2893-1256"
};
//自定义的Regex检测规则
Regex rgx = new Regex(@"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$");
foreach (string partNumber in partNumbers)
Console.WriteLine("{0} {1} a valid part number.",