C#检查汉字的正则表达式
直接上代码:
我测试的环境如下:
使用语言:C#
环境:.net Core 2.1 (其他环境一样可以使用正则表达式)
C# 调用正则表达式需要引用:using System.Collections.Generic;
检查汉字的正则表达式:[\u4e00-\u9fa5]
然后:var result = Regex.Matches(input, pattern);//C#调用正则
就会返回结果
rsult.Count
介绍: Count 这个属性:里面有多少匹配字符,在这个程序里面可以理解为检查出多少汉字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Matches
{
class Program
{
static void Main(string[] args)
{
string input = "我";//声明变量
string pattern = @"[\u4e00-\u9fa5]";//检查汉字的正则表达式
var result = Regex.Matches(input, pattern);//C#调用正则
if (result.Count>0)//Count 这个属性:里面有多少匹配字符,在这个程序里面可以理解为检查出多少汉字
{
Console.WriteLine("包含汉字");
}
else
{
Console.WriteLine("不包含汉字");
}
}
}
}
运行结果: