I require a regex that will text that a string starts with one chaecter (a-z) and is followed by at least one digit.
I have tried…
^[a-zA-Z]{1}\d+
my test data is…
a1234 (pass)
B123444434 (pass)
Z098745 (pass)
ZZ12345 (fail)
G4b553b3 (fail)
The problem is that the last two lines shoudl fail but dont, Im not sure if the problem is my regex or my c# (below);
int pass = 0;
int fail = 0;
string[] testdata =
{
"a1234",
"B1234",
"Z098745",
"ZZ12345",
"G4b5533",
};
string sPattern = "[a-zA-Z]{1}\\d+";
foreach (string s in testdata)
{
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
{
pass++;
}
else
{
fail++;
}
}
You seem to have missed
^in your code, soZ12345matches forZZ12345andb5533matches forG4b5533.And as it was mentioned,
{1}is redundant.I believe you should have
in your code.