We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
\^............... The beginning of a line
\$............... The end of a line
\w .............. A word character
\X{n,m}..... X, at least n but not more than m times
We need to place the \^ and \$ anchors at the beginning and ends of the String so that an invalid username with a valid username inside it doesn't get approved.
The extra backslash in \w is to "escape" the 2nd backslash character in a Java String.
Valid Username Regular Expression
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
\^............... The beginning of a line
\$............... The end of a line
\w .............. A word character
\X{n,m}..... X, at least n but not more than m times
We need to place the \^ and \$ anchors at the beginning and ends of the String so that an invalid username with a valid username inside it doesn't get approved.
The extra backslash in \w is to "escape" the 2nd backslash character in a Java String.
From my HackerRank solutions.