ASP.NET - Need Regular Expression - Asked By Esmail on 05-Mar-12 08:43 AM

I need regular expression for string to contain atleast
One Alphabet
One Number

and can contain any number of special characters
[)ia6l0 iii replied to Esmail on 05-Mar-12 09:52 AM
This is what I use - \A(?=.*?[0-9])(?=.*?[A-Za-z])\w+\Z

e.g. 
string inputString   = "1nput5tring";
string pattern       = @"\A(?=.*?[0-9])(?=.*?[A-Za-z])\w+\Z";
bool matched = Regex.IsMatch(inputString, pattern);


Thank you,
D Company replied to Esmail on 05-Mar-12 01:04 PM
Hi,

this works fine.

^\d*[a-zA-Z][a-zA-Z0-9]*$

use it

Regards
D
kalpana aparnathi replied to Esmail on 05-Mar-12 01:55 PM
hi,

Try this way:

if (Regex.IsMatch("Test123!@#",
        @"^(?=.*?[a-z])(?=.*?\d)(?=.*?[!@#$%\^&*\(\)\-_+=;:'""\/\[\]{},.<>|`])",
        RegexOptions.IgnoreCase))
{
  // do something
}

Regards
Somesh Yadav replied to Esmail on 06-Mar-12 01:40 AM
Hi try this,

Try these multi-lingual extensions I have made for string.

IsAlphaNumeric - String must contain atleast 1 alpha (letter in Unicode range, specified in charSet) and atleast 1 number (specified in numSet). Also, the string should comprise only of alpha and numbers.

IsAlpha - String should contain atleast 1 alpha (in the language charSet specified) and comprise only of alpha.

IsNumeric - String should contain atleast 1 number (in the language numSet specified) and comprise only of numbers.

The charSet/numSet range for the desired language can be specified. The Unicode ranges are available on below link:

http://www.ssec.wisc.edu/~tomw/java/unicode.html

API :

    public static bool IsAlphaNumeric(this string stringToTest)
   
{
       
//English
       
const string charSet = "a-zA-Z";
       
const string numSet = @"0-9";

       
//Greek
       
//const string charSet = @"\u0388-\u03EF";            
       
//const string numSet = @"0-9";

       
//Bengali
       
//const string charSet = @"\u0985-\u09E3";
       
//const string numSet = @"\u09E6-\u09EF";

       
//Hindi
       
//const string charSet = @"\u0905-\u0963";
       
//const string numSet = @"\u0966-\u096F";

       
return Regex.Match(stringToTest, @"^(?=[" + numSet + @"]*?[" + charSet + @"]+)(?=[" + charSet + @"]*?[" + numSet + @"]+)[" + charSet + numSet +@"]+$").Success;
   
}

   
public static bool IsNumeric(this string stringToTest)
   
{
       
//English
       
const string numSet = @"0-9";

       
//Hindi
       
//const string numSet = @"\u0966-\u096F";

       
return Regex.Match(stringToTest, @"^[" + numSet + @"]+$").Success;
   
}

   
public static bool IsAlpha(this string stringToTest)
   
{
       
//English
       
const string charSet = "a-zA-Z";

       
return Regex.Match(stringToTest, @"^[" + charSet + @"]+$").Success;
   
}

Usage :

        //English
       
string test = "AASD121asf";

       
//Greek
       
//string test = "Ϡϛβ123";

       
//Bengali
       
//string test = "শর৩৮";

       
//Hindi
       
//string test = @"क़लम३७ख़";

       
bool isAlphaNum = test.IsAlphaNumeric();

or

Remember in Computer Science, an Alphanumeric value means the first character is an alphabet or underscore. Thereafter the character can be 0-9, A-Z, a-z, or underscore (_). Tested under php:

$regex = '/^[A-Za-z_][A-Za-z\d_]*$/'

or take this

^[A-Za-z_][A-Za-z\d_]*$

and place it in your development language.


Hope it helps you.
Abhinav Sejpal replied to Esmail on 24-Mar-12 04:20 AM
This should do it:

if (Regex.IsMatch(subjectString, @"
    # Match string having one letter and one digit (min).
    \A         # Anchor to start of string.
    (?=.*?[0-9])   # at least one number and
    (?=.*?[A-Za-z])  # at least one letter.
    \w+        # Match string of alphanums.
    \Z         # Anchor to end of string.
    ",
    RegexOptions.IgnorePatternWhitespace)) {
    // Successful match
} else {
    // Match attempt failed
}