locked
Regular Expressiom RRS feed

  • Question

  • User-1499457942 posted

    Hi

       I have below regular expression . I want - or underscore also to be allowed

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z ]+$");
    }

    Thanks

    Wednesday, September 19, 2018 7:26 AM

Answers

  • User475983607 posted

    Hi

      It is giving error Unrecognised space sequence

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_\- ]+$");
    }

    Thanks

    This is an entirely different issue.

    Use the string literal, reference documentation, and basic debug tools. 

    return Regex.IsMatch(input, @"^[a-zA-Z_\- ]+$");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2018 6:29 PM

All replies

  • User1724605321 posted

    Hi JagjitSingh :

    Try this :

            public static bool IsAlpha(string input)
            {
                return Regex.IsMatch(input, "^[a-zA-Z_-]+$");
            }

    Best Regards,

    Nan Yu

    Wednesday, September 19, 2018 7:56 AM
  • User-1499457942 posted

    Hi

      I have below Regular Expression It is not accepting -_

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_- ]+$");
    }

    Thanks

    Thursday, September 20, 2018 6:12 PM
  • User475983607 posted

    Hi

      I have below Regular Expression It is not accepting -_

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_- ]+$");
    }

    Thanks

    The RegEx expression is invalid.  

    ^[a-zA-Z_\- ]+$

    As recommended in your other threads with the same issue, use an online RegEx tester to debug the RegEx; https://regex101.com/

    Thursday, September 20, 2018 6:19 PM
  • User-1499457942 posted

    Hi

      It is giving error Unrecognised space sequence

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_\- ]+$");
    }

    Thanks

    Thursday, September 20, 2018 6:24 PM
  • User475983607 posted

    Hi

      It is giving error Unrecognised space sequence

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_\- ]+$");
    }

    Thanks

    This is an entirely different issue.

    Use the string literal, reference documentation, and basic debug tools. 

    return Regex.IsMatch(input, @"^[a-zA-Z_\- ]+$");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2018 6:29 PM
  • User-271186128 posted

    Hi JagjitSingh,

    I have below Regular Expression It is not accepting -_

    public static bool IsAlpha(string input)
    {
    return Regex.IsMatch(input, "^[a-zA-Z_- ]+$");
    }

    Try to change the regular expression like this:

    ^[a-zA-Z_ -]+$

    The test result as below:

            protected void Page_Load(object sender, EventArgs e)
            {
                string str = "aA_asdf";
                Response.Write(IsAlpha(str));   //result: True
                string str2 = "aA-asdf";
                Response.Write(IsAlpha(str2));   //result: True
                string str3 = "aAasdf";
                Response.Write(IsAlpha(str3));   //result: True
                string str4 = "A-a_sdf";
                Response.Write(IsAlpha(str4));   //result: True
                string str5 = "A-a*sdf";
                Response.Write(IsAlpha(str5));   //result: false
                string str6 = "A-_asdf";
                Response.Write(IsAlpha(str6));   //result: True
                string str7 = "A-a sdf";
                Response.Write(IsAlpha(str7));   //result: True
            }
    
            public static bool IsAlpha(string input)
            {
                return Regex.IsMatch(input, "^[a-zA-Z_ -]+$");
            }

    Best regards,
    Dillion

    Friday, September 21, 2018 2:35 AM
  • User1724605321 posted

    Hi JagjitSingh ,

    I have below Regular Expression It is not accepting -_

    What do you mean by not accepting -_ ?

    Best Regards,

    Nan Yu

    Friday, September 21, 2018 5:23 AM