Regex Query - not sure why it is not checking for 2 letters at start?

Restrict Possible Usernames SolutionsSolution 1 (Click to Show/Hide)So…
[/quote]

Just looking at the solution to this… which i would never have got on my own!!

i dont understand point 6 & 7 of solution 1.

  1. ^[a-z] - first character is a letter
  2. \d\d+ - following characters are 2 or more digits

Wouldn’t this just ensure there is 1 letter at the start?

it says:

  1. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

is this a mistake, or am i not seeing something?

i thought it would have to be ^[a-z]+ \d*$ … do you put the + inside or outside the bracket?!

I also don’t understand why you need the | or operator either
Can this also be explained to me, please.

thanks

Please could someone clarify.

Thanks

Sure, so the | is, as you recognize, the logical or of the regex. Before the or handles two-letter cases by saying “i require a letter, followed by one or more letters (this is the two letters bit), and then zero or more digits.” So anything that starts with two or more letters and optional numbers.

But what about a76? It’s beyond the two-character cutoff, so it’s valid, but it won’t pass. This is why the or - a valid string is either two or more letters with optional digits, or a single letter followed by two or more digits. This last part is handled after the |.

Thanks for your help!

I still don’t really understand it.
I get the first part, 2 letters followed by zero or more numbers.

So surely this would mean a76 would fail? as the second character is a number.

I don’t understand why a single letter plus two or more digits will pass as the rules say “A two-character username can only use alphabet letters as characters.”

Doesn’t this mean that the first two letters have to be [a-z]?

I dont know where the last part (the or) has come from: a valid string is either two or more letters with optional digits, or a single letter followed by two or more digits. This last part is handled after the |

So the rules are saying this:

  • a single character combination fails.
  • a double character combination only passes of both characters are letters.
  • three or more, at least the first must be a letter, and any numbers must follow letters.

So a three character combo like a76 is valid. The first half of The regex handles rules one and two, after the or handles rule 3.

1 Like

Ahhh, gotcha! Thank you :slight_smile:

1 Like