Home > Back-end >  Found a solution to a problem on Hackerrank but i cant find what this syntax mean and can anyone exp
Found a solution to a problem on Hackerrank but i cant find what this syntax mean and can anyone exp

Time:01-20

{
   public static final String regularExpression = "^[a-zA-Z][a-zA-Z0-9_]{7,29}$";
}

is a java syntax that I found while solving hackerrank problems and I don't understand how the expression in double-quotes works.

To me it looks like an ordinary string that cannot be changed but what are those characters and their sequence mean in java. According to the problem input shouldn't have a number or special character in the beginning. other than that the input should link of problem:https://www.hackerrank.com/challenges/valid-username-checker/problem

link of solution i found: https://shareablecode.com/snippets/java-solution-for-hackerrank-problem-valid-username-regular-expression-bZFc-m4vJ

CodePudding user response:

"^[a-zA-Z][a-zA-Z0-9_]{7,29}$"

[a-zA-Z]: your input should start with alphabetic character
[a-zA-Z0-9_]: followed by alphanumeric character and underscore
{7,29}$: ensures the length of input is between 7 and 29

Please find below detailed explanation: https://mkyong.com/regular-expressions/how-to-validate-username-with-regular-expression/

CodePudding user response:

This is a regular expression.

Regular expressions are used to find patterns in text. That's it.

Each symbol in a regular expression has a specific role.

For example:

  • ca[ t r n ]: means that any word that starts with ca and ends with either t, r or n can match this regular expression.

    so cat, car and can are three words that match this regular expression

    whereas cap doesn't!

This article below explains clearly regular expressions:

https://dev.to/awwsmm/20-small-steps-to-become-a-regex-master-mpc

  •  Tags:  
  • Related