Check if a string contains uppercase, lowercase, special characters and numeric values
Given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print “Yes”. Otherwise, print “No”.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: str = “#GeeksForGeeks123@”
Output: Yes
Explanation:
The given string contains uppercase characters(‘G’, ‘F’), lowercase characters(‘e’, ‘k’, ‘s’, ‘o’, ‘r’), special characters( ‘#’, ‘@’), and numeric values(‘1’, ‘2’, ‘3’). Therefore, the output is Yes.
Input: str = “GeeksForGeeks”
Output: No
Explanation:
The given string contains only uppercase characters and lowercase characters. Therefore, the output is No.
Time Complexity: O(N)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
How to iterate through all characters in string then check if any characters contain lowercase, uppercase, digits, and punctuation and return True
I’m trying to check if a string contains any characters that are lowercase, uppercase, a digit, or punctuation and if any are then return True, but whenever i run it the code only checks the first character. Am i iterating incorrectly?
I’ve tried to create conditionals that check if a character is lowercase, uppercase, a digit, or punctuation, and if it is then return true. else return false.
this is what i have at the moment:
I expect it return True for the lowercase if the character contains a lowercase, and the same for the other call to functions, but the actual output is all True when it should only be true for the lowercase, digit, and punctuation
3 Answers 3
You are returning after the first iteration; plus, you aren’t using your characters argument.
You can simplify this with the any function:
Anyway, in both cases, running main will output:
Well, the problem here is that you return after checking the first character. To do what you want, you should return True only if any of the conditions are fulfilled.
If none are, then you should go to the next character, and only when you have gone through all the characters, but none fulfil those conditions, should you return :
(I typed this before the edit to the question, so I’ll just leave it here for completeness)
That, at least, is what your question says. However, given the context (you seem to be checking the validity of a password), I think that what you want is really to assert that all the characters in the string satisfy the given constraints. If so, then you would need to invert the conditions:
Detect if a string contains uppercase characters
Is there an alternative to using a regular expression to detect if a string contains uppercase characters? Currently I’m using the following regular expression:
It works fine but I often hear the old adage «If you’re using regular expressions you now have two problems».
7 Answers 7
RegEx seems to be overkill:
Although a regex this simple will probably work fine
although I suppose that in your case you’re not expecting non-ASCII letters in your string (considering its name).
using for loops, not as efficient and readable as the other methods pointed out, but for starters should work and provide a comprehensive way of doing this:
Basically, you iterate over your string and check for Upper Chars, then you can add logic as to what to do with the place where there is an Upper Char. For example, insert a space where the second upper case char is found and then use the ToLower method on the whole string.
Not the answer you’re looking for? Browse other questions tagged c# regex string or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.2.40878
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
I want a regular expression to check that:
And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character.
How can I write it for a password must be eight characters including one uppercase letter, one special character and alphanumeric characters?
39 Answers 39
Minimum eight characters, at least one letter and one number:
Minimum eight characters, at least one letter, one number and one special character:
Minimum eight characters, at least one uppercase letter, one lowercase letter and one number:
Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:
Minimum eight and maximum 10 characters, at least one uppercase letter, one lowercase letter, one number and one special character:
You may use this regex with multiple lookahead assertions (conditions):
This regex will enforce these rules:
Regular expressions don’t have an AND operator, so it’s pretty hard to write a regex that matches valid passwords, when validity is defined by something AND something else AND something else.
But, regular expressions do have an OR operator, so just apply DeMorgan’s theorem, and write a regex that matches invalid passwords:
Anything with less than eight characters OR anything with no numbers OR anything with no uppercase OR or anything with no lowercase OR anything with no special characters.
If anything matches that, then it’s an invalid password.
Just a small improvement for @anubhava’s answer: Since special character are limited to the ones in the keyboard, use this for any special character:
This regex will enforce these rules:
Use the following Regex to satisfy the below conditions:
|<>:;<>/] <8,15>which will include the following Nonalphanumeric characters: (@$!%*?&+
A more «generic» version(?), allowing none English letters as special characters.
I would reply to Peter Mortensen, but I don’t have enough reputation.
His expressions are perfect for each of the specified minimum requirements. The problem with his expressions that don’t require special characters is that they also don’t ALLOW special characters, so they also enforce maximum requirements, which I don’t believe the OP requested. Normally you want to allow your users to make their password as strong as they want; why restrict strong passwords?
So, his «minimum eight characters, at least one letter and one number» expression:
achieves the minimum requirement, but the remaining characters can only be letter and numbers. To allow (but not require) special characters, you should use something like:
^(?=.*[A-Za-z])(?=.*\d).<8,>$ to allow any characters
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d$@$!%*#?&]<8,>$ to allow specific special characters
Likewise, «minimum eight characters, at least one uppercase letter, one lowercase letter and one number:»
meets that minimum requirement, but only allows letters and numbers. Use:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d$@$!%*?&] <8,>to allow specific special characters.
Regular expression to check if password is «8 characters including 1 uppercase letter, 1 special character, alphanumeric characters»
I want a regular expression to check that
a password must be eight characters including one uppercase letter, one special character and alphanumeric characters.
And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character.
How I can write it for a password that must be eight characters including one uppercase letter, one special character and alphanumeric characters?
14 Answers 14
The regular expression you are after will most likely be huge and a nightmare to maintain especially for people who are not that familiar with regular expressions.
I think it would be easier to break your regex down and do it one bit at a time. It might take a bit more to do, but I am pretty sure that maintaining it and debugging it would be easier. This would also allow you to provide more directed error messages to your users (other than just Invalid Password ) which should improve user experience.
From what I am seeing you are pretty fluent in regex, so I would presume that giving you the regular expressions to do what you need would be futile.
Seeing your comment, this is how I would go about it:
Alphanumeric characters: Using the \w+ should match any letter and number and underscore.
Take a look at this tutorial for more information.














