如何仅使用C#为数字编写正则表达式?
正则表达式是可以与输入文本匹配的模式。
.Net框架提供了允许此类匹配的正则表达式引擎。
模式由一个或多个字符文字,运算符或构造组成。
这是RegEx使用的基本模式元字符-
* = zero or more ? = zero or one ^ = not [] = range
^符号用于指定非条件。
如果要给出范围值(例如0-9或az或AZ),请使用[]括号
示例
class Program{
public static void Main(){
string num = "123dh";
Regex regex = new Regex(@"^-?[0-9][0-9,\.]+$");
var res = regex.IsMatch(num);
System.Console.WriteLine(res);
}
}输出结果
False
示例
class Program{
public static void Main(){
string num = "123";
Regex regex = new Regex(@"^-?[0-9][0-9,\.]+$");
var res = regex.IsMatch(num);
System.Console.WriteLine(res);
}
}输出结果
True
示例
class Program{
public static void Main(){
string num = "123.67";
Regex regex = new Regex(@"^-?[0-9][0-9,\.]+$");
var res = regex.IsMatch(num);
System.Console.WriteLine(res);
}
}输出结果
True
热门推荐
5 破五祝福语简短的
10 赛前祝福语加姓名简短
11 国庆中秋祝福语大全简短
12 牛年大气简短的祝福语
13 新年祝福语大全简短成语
14 小朋友得奖祝福语简短
15 简短元宵节祝福语
16 鼠年同事祝福语简短创意
17 关于糖果的祝福语简短
18 留学生祝福语简短