C# 我如何使函数Regex工作?
早上好, 我正在为我的intership做一个页面,我发现了这个函数,但它不起作用,有人能帮我吗? 先谢谢你C# 我如何使函数Regex工作?,c#,asp.net,C#,Asp.net,早上好, 我正在为我的intership做一个页面,我发现了这个函数,但它不起作用,有人能帮我吗? 先谢谢你 string _id = this.txtIdGrupo.Text; if (!Regex.IsMatch(_id, @"^\d+$")) return false; 结果表明: error CS0103: The name 'Regex' does not exist in the current context 这不是您显示的代码的问题 这是文
string _id = this.txtIdGrupo.Text;
if (!Regex.IsMatch(_id, @"^\d+$"))
return false;
结果表明:
error CS0103: The name 'Regex' does not exist in the current context
这不是您显示的代码的问题 这是文件顶部的名称空间问题 解决办法应该是 在行动中看到它: 请注意使用System.Text.RegularExpressions 使用诸如Resharper和类似的工具肯定会给你提示,并会改善你的日常生活 确保您拥有System.Text.RegularExpressions
是否已导入当您对该错误代码进行web搜索时,您学到了什么?@adrianhh-您所说的web搜索是什么?使用System.Text.RegularExpressions?哦,好的,实际上我没有,谢谢你,没有问题,我会搜索那张通行证的,谢谢
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
var isNumeric1 = IsNumeric("1");
Console.WriteLine(isNumeric1);
var isNumeric2 = IsNumeric("HelloWorld");
Console.WriteLine(isNumeric2);
//call IsNumeric with the value of this.txtIdGrupo.Text like this
//var isNumeric = IsNumeric(this.txtIdGrupo.Text);
}
private static bool IsNumeric(string str)
{
string id = str;
if (!Regex.IsMatch(id, @"^\d+$"))
return false;
return true;
}
}