|
I am new to reqular expressions. I am searching for the following:
1. A regular expression that ensures a string containing numeric and text (any character) that has at least a length of 8 2. A regular expression that ensures a string containing numeric and text (any character) that has at least one number 3. A regular expression that ensures a string containing numeric and text (any character) that has at least one lowercase character (a-z) 4. A regular expression that ensures a string containing numeric and text (any character) that has at least one uppercase character (A-Z) 5. all of the above!
I apologise if this has all been asked before but the syntax of the reg expressions are brand new to me and i need to get this done asap.
I am using a regularexpression calidator on Visual Studio 2008, language is vb.
Thanks,
L | | LiamLynch1982 | Do you mean to say "only"? 1. ^[a-zA-Z0-9]{8,}$ 2. ^(?=.*\d.*$)[a-zA-Z0-9]{8,}$ 3. ^(?=.*[a-z].*$)[a-zA-Z0-9]{8,}$ 4. ^(?=.*[A-Z].*$)[a-zA-Z0-9]{8,}$ >>>5. all of the above! 5. ^(?=.*\d.*$)(?=.*[a-z].*$)(?=.*[A-Z].*$)[a-zA-Z0-9]{8,}$ NOTE: the {8,} matches "at least eight". The ^ and $ will require the pattern to match the whole string or else fail. If you want to "find" strings that meet your requirements within a larger string, that requires some modification to the patterns. Let us know.
Les Potter, Xalnix Corporation, Yet Another C# Blog- Marked As Answer byLiamLynch1982 Friday, September 11, 2009 1:11 PM
-
| | xalnix | hey, try something like this:
Regex.IsMatch("s0m1t2xt", "[a-z0-9]{8}")
in general, {number} used to specify minimum expected substring length, [] used to specify allowed chars
S.G. | | Sergey Galich | 1. A regular expression that ensures a string containing numeric and text (any character) that has at least a length of 8
[0-9]{8}
2. A regular expression that ensures a string containing numeric and text (any character) that has at least one number
(?=.*\d.*$)\w+
3. A regular expression that ensures a string containing numeric and text (any character) that has at least one lowercase character (a-z)
(?=[a-z].*$)(?=.*\d.*$)\w+
4. A regular expression that ensures a string containing numeric and text (any character) that has at least one uppercase character (A-Z)
(?=[A-Z].*$)(?=.*\d.*$)\w+
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | 1. A regular expression that ensures a string containing numeric and text (any character) that has at least a length of 8
[0-9]{8}
3. A regular expression that ensures a string containing numeric and text (any character) that has at least one lowercase character (a-z)
(?=[a-z].*$)(?=.*\d.*$)\w+
4. A regular expression that ensures a string containing numeric and text (any character) that has at least one uppercase character (A-Z)
(?=[A-Z].*$)(?=.*\d.*$)\w+
Thanks for your reply John, very useful, can i just clarify:
your solution to 1. above, is that only for numeric strings with more than 8 digits, doesn't seem to work for alphanumeric strings with a length of 8 or more
your solution to 3 and 4, do i require (?=.*\d.*$)\w+ as that seems to check for an instance of a number when all i need is an instance of an uppercase letter in 4 or a lowercase letter in 3 | | LiamLynch1982 | "your solution to 1. above, is that only for numeric strings with more than 8 digits, doesn't seem to work for alphanumeric strings with a length of 8 or more"
Yes, good catch, sorry about that.
[0-9a-zA-Z]
"your solution to 3 and 4, do i require (?=.*\d.*$)\w+ as that seems to check for an instance of a number when all i need is an instance of an uppercase letter in 4 or a lowercase letter in 3"
You can modify that to meet your goal, I think you get the idea now.
Good job.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Do you mean to say "only"? 1. ^[a-zA-Z0-9]{8,}$ 2. ^(?=.*\d.*$)[a-zA-Z0-9]{8,}$ 3. ^(?=.*[a-z].*$)[a-zA-Z0-9]{8,}$ 4. ^(?=.*[A-Z].*$)[a-zA-Z0-9]{8,}$ >>>5. all of the above! 5. ^(?=.*\d.*$)(?=.*[a-z].*$)(?=.*[A-Z].*$)[a-zA-Z0-9]{8,}$ NOTE: the {8,} matches "at least eight". The ^ and $ will require the pattern to match the whole string or else fail. If you want to "find" strings that meet your requirements within a larger string, that requires some modification to the patterns. Let us know.
Les Potter, Xalnix Corporation, Yet Another C# Blog- Marked As Answer byLiamLynch1982 Friday, September 11, 2009 1:11 PM
-
| | xalnix | Learn something new every day. I didn't know you could do that Les {8,}
Nice to know...Thanks! John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Thanks Les, I meant at least one. The password criteria are as follows
- Must be at least 8 characters
- Must contain at least one one lower case letter, one upper case letter, and one digit
A list of passwords that shuld pass would be Passw0rd, p8ssw0Rd, PA$*SSW0s etc
The solution to 1 doesn't work if the user inputs a special character, any way to include all the character set?
2, 3, 4 also don't seem to pass with the above examples.
Sorry for bothering you guys and thanks for everything to date
| | LiamLynch1982 | Yes, just put the other charactes you want between the [ ]. See the example C# code below...
string pattern1 = @"^[a-zA-Z0-9$*]{8,}$";
string pattern2 = @"^(?=.*\d.*$)[a-zA-Z0-9$*]{8,}$";
string pattern3 = @"^(?=.*[a-z].*$)[a-zA-Z0-9$*]{8,}$";
string pattern4 = @"^(?=.*[A-Z].*$)[a-zA-Z0-9$*]{8,}$";
string pattern5 = @"^(?=.*\d.*$)(?=.*[a-z].*$)(?=.*[A-Z].*$)[a-zA-Z0-9$*]{8,}$";
string [] tests = {
"Passw0rd",
"p8ssw0Rd",
"PA$*SSW0s",
};
foreach (string test in tests)
{
Console.WriteLine("{0}: {1}", Regex.IsMatch(test,pattern5).ToString(), test);
}
Les Potter, Xalnix Corporation, Yet Another C# Blog | | xalnix | Thanks Les,
I know it finds the instance of uppercase, lowercase etc but I am using a regular expression validator in Visual Studio and it still flags up as invalid. not to worry, you have been a great help, thanks to you and john | | LiamLynch1982 |
|