Please don't do that... little Unicode BABY ANGELs like this one 👼 are dying! ◕◡◕ (← these are not images) (nor is the arrow!)
☺
And you are killing 20 years of DOS :-) (the last smiley is called WHITE SMILING FACE... Now it's at 263A... But in ancient times it was ALT-1)
and his friend
☻
BLACK SMILING FACE... Now it's at 263B... But in ancient times it was ALT-2
Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" 0-9 digits.)
Regex for all special characters
Regex Expression for string which should contain only limited type of special characters
Regex to find and output all special characters in a string
regular expression - Regex for Password. Restricting Special Characters - Unix & Linux Stack Exchange
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
Yes, your expression will match any string that includes at least one of the specified special characters
hi all,
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
thanks,
rob
Regex:
^(?=[a-zA-Z0-9#@$?]{8,}$)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Explanation:
In the regex you provided, the lookaheads serves to ensure your string met some specific conditions, but they are not the real filter which keeps out undesired strings. You specified the following conditions:
(?=.*?[A-Z]): Match at least one uppercase letter.(?=.*?[a-z]): Match at least one lowercase letter.(?=.*?[0-9]): Match at least one number.(?=.*?[#@$?]): Match at least one of these characters:#@$?.{8,}: Match any character at least 8 times.
But there are at least two flaws:
The
(?=.*?[#@$?])part is unnecessary because those special characters are meant to be optional, not mandatory[1].As I said before, you did not specify a real filter, so thanks to the
.{8,}part, your regex will accept any string as long as it meets the conditions established by the lookaheads, even if it has undesired special characters[1].
So to solve those flaws it is necessary to:
Delete the
(?=.*?[#@$?])part.Add a new lookahead that acts as the filter mentioned above.
To construct this filter, you should think "which characters I want to allow?" instead of "which characters I want to disallow?", because that is a easier scenario to handle in this specific case. If you say you only want a-z, A-Z, 0-9 and #@$? to be your allowed characters, then the lookahead should look like this:
(?=[a-zA-Z0-9#@$?])
But hey, in this step you can even set the minimum length and tell the lookahead where to start and where to stop (the start and end of string in this case):
(?=[a-zA-Z0-9#@$?]{8,}$)
I omitted the ^ here because it's already at the beggining of everything, so it's not necessary to be redundant. Now we just bring together all the lookaheads and match the valid password using .* instead of .{8,}:
^(?=[a-zA-Z0-9#@$?]{8,}$)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Note:
- Although, in the regex and examples you provided, I don't really know why: 1)
#@$?were not treated as mandatory; 2) undesired characters were only allowed at the end of the string and not in another place. Maybe it has something to do with the regex engine used by AWS, because everything worked as expected when I tested it on my own.
We want a lookahead anchored at the string start for each required character type as well as the length requirement. Then a simple .* to slurp it all up:
^(?=[0-9a-zA-Z#@\$\?]{8,}$)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9]).*
Explanation:
First off, I decided to avoid use of the lazy quantifier when matching required char types (e.g. one uppercase) for the following reasons:
- They are expensive.
- Different regex engines have different ways of signifying lazy. (A couple don't support it at all.)
- They aren't as common/familiar as the alternative.
So for efficiency, readability and "portability" I'm using the ^[^x]*[x] construct.
Now breaking the rest down...
^ : Everything anchored to the start
(?=[0-9a-zA-Z#@\$\?]{8,}$) : Lookahead with 8 or more of your allowed characters between start and end of string.
The next three use the same pattern: a lookahead matching zero or more of a char not matching a required char, then the required char. These are all anchored to the beginning so the effect is to allow a match of the required char at any position in the string:
(?=[^a-z]*[a-z]) : At least one lowercase.
(?=[^A-Z]*[A-Z]) : At least one uppercase.
(?=[^0-9]*[0-9]) : At least one digit.
.* : Everything above is lookahead which doesn't consume anything so consume it all here. The first lookahead makes sure the entire string is valid chars so this is safe.
I make no claims about this being optimized (except for avoiding lazy quantifier). This is simply one of the easier forms to comprehend.
Note: the cause of the problem you observed with #@$? is due to the lookahead not being anchored to the end of string. Any character will match after one of those four (and not necessarily just in the last position). Of course, you can't just add $ since that then crowds out valid characters. That's why I include all valid characters in the same lookahead.
Yes, you can. That should work.
.= any char except newline\.= the actual dot character.?=.{0,1}= match any char except newline zero or one times.*=.{0,}= match any char except newline zero or more times.+=.{1,}= match any char except newline one or more times
Yes that will work, though note that . will not match newlines unless you pass the DOTALL flag when compiling the expression:
Pattern pattern = Pattern.compile(".*123", Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches();
Wondering if there is a clean way to do this where you could use \W instead of spelling out every one of the allowed special characters.
[A-Za-z0-9@!#$%&*()_+-=//.,";:{}|...etc etc] for example, is so ugly but it works to exclude spaces from a password validation while allowing other special characters, letters, and numbers.
I have not been successful in finding a way to neaten it up to [A-Za-z0-9\W] with something short and sweet to say "except for spaces" at the beginning, middle, end.
Any thoughts? Thanks!