none
Testing Regex for Alphanumeric Value validation RRS feed

  • Question

  • Hi,

    I am using regex validator in our code. This validator is working for some of the Regular Expressions like validating CC and Bank Account number. But method is not working to Validate Alphanumeric values like (D23145890 or XJ13256141).

    My code is as below - 

     foreach (var regex in m_aRegexes)
                {
                    Regex oRegex = (Regex) regex;

                    mc = oRegex.Matches(m_sTxt);

    if(mc.Count > 0)

    Return True;

    Else

    Return false;

    }

    Working Regex - 

    1) (^[a-z0-9][-a-z0-9._]+@([-a-z0-9]+\.)+[a-z]{2,5}$)

    2) (\d{3}-\d{3}-\d{4}|^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$)

    Non Working Regex -

    1) (^(?!^0+$)[a-zA-Z0-9]{3,20}$)

    2) (^E\d{6}?$)

    Can somebody please help me here?


    Abhijeet Khopade

    Monday, July 30, 2018 12:53 PM

Answers

  • I suspect the issue is with "(?!^0+$)", but I am not certain. What is that for? Making sure that you don't match a string that is all zeros? Anyway, I would pick apart the clauses of the expression and see what you get.

    Try "(^(?!^0+$)"  and "[a-zA-Z0-9]{3,20}$)" separately and see what happens with each.

    Also check for leading and trailing spaces in the input string. They would cause failure.
    mc = oRegex.Matches(m_sTxt.trim());

    Ethan


    Ethan Strauss

    Tuesday, July 31, 2018 3:35 PM

All replies

  • bool found = false;
    foreach (var regex in m_aRegexes)
    {
       Regex oRegex = (Regex) regex;
       mc = oRegex.Matches(m_sTxt);
    
       if(mc.Count > 0)
          found = true;
    }
    
    return found;

    Tuesday, July 31, 2018 1:50 AM
  • ritehere40,

    Yeah, okay. Fair point.

    In my defence, I wasn't trying to write the most optimised code, just enough to show where (I believe) the problem lies.

    Tuesday, July 31, 2018 2:32 AM
  • Hi,

    Thank you both, but my problem is not about the how and where to put condition, my problem is finding ALPHANUMERIC values in string. e.q. 'We are looking for Alphanumeric values that could be PT327891 or M80974121. There are some employee codes we need to extract using "E983201" format.'

    In the above string I was looking for values starting with PTNNNNNNN or LXNNNNNNN or ENNNNNN where PT,L and E is constant and my Regex is not working for that. 

    So my code should look for combination of alphanumeric pattern whether it is start of the string, middle of the string or end of the string. This string could be text file as well.

    1) (^(?!^0+$)[a-zA-Z0-9]{3,20}$)

    2) (^E\d{6}?$)


    Abhijeet Khopade


    Tuesday, July 31, 2018 1:29 PM
  • I suspect the issue is with "(?!^0+$)", but I am not certain. What is that for? Making sure that you don't match a string that is all zeros? Anyway, I would pick apart the clauses of the expression and see what you get.

    Try "(^(?!^0+$)"  and "[a-zA-Z0-9]{3,20}$)" separately and see what happens with each.

    Also check for leading and trailing spaces in the input string. They would cause failure.
    mc = oRegex.Matches(m_sTxt.trim());

    Ethan


    Ethan Strauss

    Tuesday, July 31, 2018 3:35 PM
  • If you want to extract, not to validate, then try an expression like this:

       \b(M\d{8}|(PT|E)\d{6})\b

    or

       \b(M\d{8}|PT\d{6}|E\d{6})\b

     

    With proper program, it will find and extract PT327891, M80974121 and E983201. Adjust it for all of the cases.



    • Edited by Viorel_MVP Tuesday, July 31, 2018 3:41 PM
    Tuesday, July 31, 2018 3:39 PM
  • Greetings Abhijeet.

    The point I was trying to make - which I probably should have made a bit more clearly - is that if you have several entries in m_aRegexes, your code will only ever check the first one. That is, it checks mc.Count and then returns no matter what the result, so it never gets to the next regex in the list.

    So if the first regex is working but the rest are not, that could be why.

    Tuesday, July 31, 2018 11:52 PM
  • Hi Ante,

    Thanks a lot for clarification. Good point appreciate that, I could resolve future issue with your suggestion. I made necessary changes to the code as per your suggestion.


    Abhijeet Khopade

    Tuesday, August 7, 2018 2:20 PM