0

In my code, I've something like this to check if the new username entered is correct or not

private static final String regularExpression = "^[aA-zZ]\\w{7,29}$";

but while providing username with _ (underscore) like _vaibhav27, the regular expression is taking it as valid entry. I want that entry to be invalid. I tried several changes, nothing worked. Can you please help?

Vaibhav Atray
  • 208
  • 4
  • 14
  • 3
    What do you think `[aA-zZ]` does/represents? What makes you think so? – Pshemo May 02 '21 at 11:37
  • ^ represents that starting character of the string. [aA-zZ] makes sure that the starting character is in the lowercase or uppercase alphabet. @Ps – Vaibhav Atray May 02 '21 at 11:45
  • 2
    See what `[A-z]` matches [here](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret) I think you want `[A-Za-z]` instead. – The fourth bird May 02 '21 at 11:45
  • 3
    `-` operator inside character class is indeed *range* operator. But it uses *single* characters to represent "borders" of that range. `aA` is not *single* character. To represent range of lowercase characters you need `a-z`, to represent range of uppercase characters you need `A-Z`. To combine both you can write `[a-zA-Z]`. `[aA-zZ]` means `a` OR `range between A-z` OR `Z`. Now notice what characters are between `A` and `z` in Unicode Table https://unicode-table.com/en/ because those will be accepted by `A-z` range. – Pshemo May 02 '21 at 11:55

0 Answers0