I'm using AWS Cognito for authenticating my application.As per the AWS Cognito guidelines, a proper username should follow the regular expression as follows [\p{L}\p{M}\p{S}\p{N}\p{P}]+ What does this regular expression mean?
Asked
Active
Viewed 1.4k times
16
-
3i upvoted because the specificity of this question is actually useful. AWS Cognito HATES spaces and will throw this error if you submit a username with a trailing space. I suspect that was the problem OP was facing. So for the sake of posterity, to dismiss this error you might need to `trim()` your username payload before dispatching to AWS – JP Lew Jul 08 '18 at 02:17
-
Oh, where did you find those guidelines? Share the link please. – eugenekr Jan 21 '23 at 10:28
1 Answers
15
This expression allows almost any kind of character and must have at least 1 character to be inputted.
If you put this regular expression through regex101.com, it will tell you what each expression is used for.
So for your one:
\p{L} matches any kind of letter from any language.
\p{M} matches a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)
\p{S} matches any math symbols, currency signs, dingbats, box-drawing characters, etc.
\p{N} matches any kind of numeric character in any script.
\p{P} matches any kind of punctuation character.
'+' Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed

Community
- 1
- 1

MinistryOfChaps
- 1,458
- 18
- 31
-
9in other words, it can contain pretty much everything except white space characters – JP Lew Jul 08 '18 at 02:19
-
-
1https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolidentityprovider.html in ProviderName, you can see the pattern that the String should match, it is a string with no spaces – stamstam May 18 '20 at 17:47
-
1I'm not sure what the regex was when @stamstam left their comment, but currently the regex doesn't allow to start or end with an underline, and the afore mentioned "almost anything", but as the 2nd character: `[^_][\p{L}\p{M}\p{S}\p{N}\p{P}][^_]+` -- I almost suspect this is wrong? – Quantum Mechanic Apr 30 '23 at 09:10
-