String program #14 (code
to create string contain atleast 2 alphabet, 2 number, 2 special length, min 8
using regex)
Enter String: P4%_$23aTsU/
Perfect string matching all criteria
The program for the string is written
in C# programming language and will accept a password as input. The logic is to
check whether password contain 2 alphabet, 2 number, 2 special characters and
minimum password of 8.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
using System.Text.RegularExpressions;
namespace patternProblem.String
{
class String14 //check string contain atleast 2
alphabet, 2 number, 2 special length, min 8 using regex
{
public static void Main()
{
//string str =
"Tes$f_use2d1";
Console.Write("Enter String:");
string str = Console.ReadLine();
string strAlphabeRegex = "(?=(.*[a-zA-Z]){2})";
string strNumberRegex = "(?=(.*[0-9]){2})";
string strSpecialCharRegex = @"(?=(.*[`!@#$%\^&*\-_=\+'/\.,]){2})";
string strMainPattern = @"(?=(.*[a-zA-Z]){2})(?=(.*[0-9]){2})(?=(.*[`!@#$%\^&*\-_=\+'/\.,]){2})(?=^.{8,15}$)";
//Using Regex
if (Regex.IsMatch(str, strAlphabeRegex))
{
if (Regex.IsMatch(str, strNumberRegex))
{
if (Regex.IsMatch(str, strSpecialCharRegex))
{
if (Regex.IsMatch(str, strMainPattern))
{
Console.WriteLine("Perfect string matching all
criteria");
}
else
{
Console.WriteLine("String length should be 8 to 15");
}
}
else
{
Console.WriteLine("String must contain atleast 2 special characters");
}
}
else
{
Console.WriteLine("String must contain atleast 2 numbers");
}
}
else
{
Console.WriteLine("String must contain atleast
2 characters");
}
}
}
}
Output:
The input
string1 here is password. So, based on the input, program will check whether
string1 contain searched string. The output is shown below:
Enter String: P4%_$23aTsU/
Perfect string matching all criteria
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Keep learning and sharing...
No comments:
Post a Comment