简体   繁体   中英

Error in Regular expression

I need to write a regular expression(in a contest actually) for a username such that

  1. The username can contain alphanumeric characters and/or underscores(_).

  2. The username must start with an alphabetic character.

  3. 8 ≤ |Username| ≤ 30.

Now the regex I have written is

String pattern = "(^[a-zA-Z][a-zA-Z0-9_]{8,30}$)";

It's showing error in some of the testcases and the correct solution of the problem is

String pattern = "(^[a-zA-Z][a-zA-Z0-9_]{7,29}$)";

Now I can't understand why {7,29} is used instead of {8,30} because the username must be between 8 to 30 characters?

And the other question why is it necessary to give ^ at the beginning and $ at last of the expression?(I know they act as boundaries but couldn't understand how? So if anyone could explain)

Here is your correct regex:

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

The {7,29} quantifier refers to the character matching [a-zA-Z0-9_] . So this means that between 7 and 29 (inclusive) of this of character must be present. In addition, there is a first character which must match [a-zA-Z] . Adding this to the range gives between 8 and 30 characters (inclusive).

For your second question, the ^ and $ symbol are boundary markers, marking the beginning ( ^ ) and end ( $ ) of the match. If you did not use these markers, then your regex would also match words longer than 30 characters, so long as a substring of that word contained the pattern which your regex matches.

Here is a demo regex which shows that a string longer than 30 characters will match your pattern if we remove the boundary markers:

Regex101

Try adding the boundary markers ( ^ and $ ) again and you will see that it will then fail.

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

Here is how you read your regex. ^ = the start of string, [a-zA-Z] = followed by one of any lowercase character between a to z or any uppercase character between A to Z, [a-zA-Z0-9_]{7,29} = followed by one of any lowercase character between a to z or any uppercase character between A to Z or any single digit number between 0 to 9 or an underscore (occurring minimum 7 times to maximum 29 times, $ = followed by the end of string

It's more easy to understand if you try to say it like "this" followed by "this" followed by "this".... etc...

Once you understand it, you got the answer to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM