Regex regExp = new Regex("[^0-9a-zA-Z-_]");这行代码就是允许哪些字符,最前面的^的意思是取反的意思,就是有不同于这些字符的都会进入if语句。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
Regex regExp = new Regex("[^0-9a-zA-Z-_]");
if (regExp.IsMatch(str))
{
Console.WriteLine("123\n");
}
}
}
}