locked
regular expression only for alphanumeric RRS feed

  • Question

  • Hi All,

    I very new to regular expressions.

    I read that \w is for checking the alphanumeric.

    So I need to check a string is aphanumeric or not.

    But when I tried like below its showing success eventhough I put special character  "@" at the end.

    I am bit confused now. Can you please help me to clarify this dooubt

                Match match2 = Regex.Match("123ab123@", @"\w");
                if (match2.Success)
                    Console.WriteLine("its matching");

    Thanks

    Friday, August 26, 2016 12:13 PM

Answers

  • Hi,

    It's not correct Regx to check entire alphanumeric string because it checks only single alphanumeric character including _(underscore) in input string so have a look on below for same.

    Match match2 = Regex.Match("123ab123", "^[a-zA-Z0-9]*$");
     if (match2.Success)
       Console.WriteLine("its matching");
    The \w metacharacter is used to find a word character in given string. A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.


    Friday, August 26, 2016 12:27 PM

All replies

  • Hi,

    It's not correct Regx to check entire alphanumeric string because it checks only single alphanumeric character including _(underscore) in input string so have a look on below for same.

    Match match2 = Regex.Match("123ab123", "^[a-zA-Z0-9]*$");
     if (match2.Success)
       Console.WriteLine("its matching");
    The \w metacharacter is used to find a word character in given string. A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.


    Friday, August 26, 2016 12:27 PM
  • Hi,

    but the main problem, that you solved in your reply without explaining it:

    If you just check for "\w", then it is looking for exactly one alphanumeric sign anywhere in the given string.

    If you want to match the whole given string, then you need to use "^" for the start and "$" for the end. And if a character can be there any number of times, you have to add a * behind it.

    So you could use "^\w*$" in your check (in case that the _ is also ok for you. or you replace \w with [a-zA-Z0-9] if you do not want to have the _ character.

    With kind regards,

    Konrad

    Friday, August 26, 2016 12:56 PM
  • Yes, and where is the problem with that? It was not specified if that is a problem or not. It is exactly what I wrote: "Any number of times".

    If something else is required, then something different must be taken. I mainly explained the solution given by Sandeep so that it is easier to understand what he changed in his regular expression.

    With kind regards,

    Konrad

    Saturday, August 27, 2016 4:47 AM
  • Sorry, but your expression is simply wrong. Please test it or just read about regular expressions.

    Hint: You can test on https://regex101.com/.

    I don't know what problem you have right now, but you act quite strange. Your contribution here is of course simply wrong and is not doing what you are saying. (And that | lists alternatives is something quite easy to remember....)

    If you want to exclude a character, you might want to have something like [^_]. So if the _ is a problem, a possible solution was given already. So to exclude the empty string you can simply replace the * with {1,} so that at least 1 character is required.

    With kind regards,

    Konrad

    Saturday, August 27, 2016 5:01 AM
  • Yes, I read your "code" and replied already to your suggestion which is simply completly wrong. 

    Saturday, August 27, 2016 5:13 AM
  • Here my purpose is to find a string is alpahnumeric or not

    That means If I pass a string , a check has to be done to validate it as aphanumeric.

    If any special character is present it has to identify that.

    Thanks,

    Saturday, August 27, 2016 5:29 AM
  • Yorry, but you proove that you cannot behave. I missed that you had a uppercase W. It is ok to search for any unwanted character and then negate. But that is simply some other logic.

    But please stop to insult others. My explanations are correct and I also provided a working solution. So please: What is you problem? (Except that you cannot behave yourself and you are simply unprofessional?)

    Konrad

    Saturday, August 27, 2016 5:41 AM
  • Hi techasuran,

    now you have 2 possible solutions:

    a) You can check that the string only contains allowed characters:

    • ^[a-zA-Z0-9]*$ (will also allow the empty string. Explaation is in my first reply.)
    • ^[a-zA-Z0-9]{1,}$ (will not allow the empty string

    b) Or you could check, if the string contains any unwanted characters

    • \W|_|^$

    The | simply gives multiple options. The \W the oposite of \w ([^\w]). So any character that is not [a-zA-Z0-9_]. The _ is just the underscore and ^$ is the empty string (String start and string end). So if there is any of these character in the string, then you get a success. That is why you have to negate the result here.

    With kind regards,

    Konrad

    Saturday, August 27, 2016 5:47 AM
  • Yes, here you was correct because I missed a small thing. Shit happens.

    But you have a big problem in your logic: The fact that I was incorrect in judgeing your reply does not make my other replies wrong (So I explained the given solution so that the user who asked had a chance to understand.

    And the fact that you are correct here simply does not mean that you are always correct. So in the other thread you simply forgot the pointers of unsafe C# (which is part of C# and part of the CLS).

    And you are completly forgetting why we are here. We are not here to be "right". We are here to help others. So it is always good to explain stuff (which you didn't) and in the other thread the user found a solution after I wrote some C# code with a simple error inside that the user fixed himself and where you replied some crappy stuff about my use of the Length Property of an array.
    So the important thing is, that the user, who comes to the forum, gets some help. 
    You could help people a lot - just try to act more professional (Insulting people is not professional!).

    With kind regards,

    Konrad

    Saturday, August 27, 2016 6:04 AM
  • Hi Techasuran,

    Here my purpose is to find a string is alpahnumeric or not

    Please have a look on my first post which check a string is either alphanumeric or not.

    If any special character is present it has to identify that.

    Please have a look on below code which else code block explains that how to identity character in a give string which are not alphanumeric.

    using System;
    using System.Text.RegularExpressions;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputString = "123a@123!";
                Match match2 = Regex.Match(inputString, "^[a-zA-Z0-9]*$");
                if (match2.Success)
                {
                    Console.WriteLine("its matching");
                }
                else
                {
                    char[] inputStringArray = inputString.ToCharArray();
                    inputStringArray = Array.FindAll<char>(inputStringArray, (c => !(char.IsLetterOrDigit(c) || char.IsWhiteSpace(c))));
                    string nonAlphanumeric = new String(inputStringArray);
                    Console.WriteLine(nonAlphanumeric);
                }
                Console.ReadKey();
            }
        }
    }
    

    Saturday, August 27, 2016 8:40 AM