C# 在C中使用来自另一个方法的正则表达式#

C# 在C中使用来自另一个方法的正则表达式#,c#,.net,regex,methods,C#,.net,Regex,Methods,我将RegularExpression()方法与包含所有if语句的validate()方法分开。如果像这样分开,我怎么能使用我的正则表达式代码 我对编程相当陌生,我还在学习如何使用方法 public void Validate() { RegularExpression(); if (PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text) == false) {

我将RegularExpression()方法与包含所有if语句的validate()方法分开。如果像这样分开,我怎么能使用我的正则表达式代码

我对编程相当陌生,我还在学习如何使用方法

 public void Validate()
    {

        RegularExpression();

      if (PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text) == false) 
            {
                MessageBox.Show("Invalid cellphone number");
            }

      if (Email_Regex.IsMatch(Email_txBox.Text) == false) 
            {
                MessageBox.Show("Invalid E-Mail");
            }
    }

 public RegularExpression(object PhoneNumber_Regex)
    {
       var PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");

       var Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");

    }

添加一个静态类,在其中声明正则表达式:

public static class MyRegexps
{
    public static readonly Regex PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
    public static readonly Regex Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");
}
然后在调用方方法中使用它们:

if (MyRegexps.PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text) == false) 
{
    MessageBox.Show("Invalid cellphone number");
}

我应该在这条线上做点什么

public static class MyValidator
{
  protected static PhoneNumberRegex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
  protected static EmailRegex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");  

  public static bool ValidatePhoneNumber(string strToMatch)
  {
    return PhoneNumberRegex.IsMatch(strToMatch);
  }

  public static bool ValidateEmail(string strToMatch)
  {
    return EmailRegex.IsMatch(strToMatch);
  }
}
然后像这样使用它

if (!MyValidator.ValidatePhoneNumber(PhonNumb_txBox.Text)) 
{
    MessageBox.Show("Invalid cellphone number");
}
  • 虽然这不是在方法之间共享内容的唯一方法,但在这种特殊情况下,使用类级成员是有意义的
  • 正则表达式本身不太可能更改,并且可能是静态的
  • 在构造函数中初始化它们,这样就可以自动执行,而不必手动调用初始值设定项
  • 所有这些加起来更像这样:

    public class MyClass
    {
        private static Regex PhoneNumber_Regex { get; set; }
        private static Regex Email_Regex { get; set; }
    
        static MyClass
        {
            PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
            Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");
        }
    
        public void Validate()
        {
            if (!PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text)) 
                MessageBox.Show("Invalid cellphone number");
            if (!Email_Regex.IsMatch(Email_txBox.Text)) 
                MessageBox.Show("Invalid E-Mail");
        }
    }
    

    您声明了2个变量-但范围意味着这些变量不存在于该调用之外,因此它们不可用

    但是,如果作为您声明的类的一部分

    readonly Regex Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");
    
    因此,您有一个只读变量,然后可以像您所想的那样将其用作该类中任何函数的一部分

      if (Email_Regex.IsMatch(Email_txBox.Text) == false) 
      {
          MessageBox.Show("Invalid cellphone number");
      }
    
      if (Email_Regex.IsMatch(Email_txBox.Text) == false) 
      {
          MessageBox.Show("Invalid cellphone number");
      }