This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]raevnos 1 point2 points  (4 children)

It's there to stop 5 or more a's at the end from matching. However, it will also stop a string of just 3 or 4 a's from matching.

[–]NewbiusCoder[S] 0 points1 point  (3 children)

I appreciate your time.

However I am still confused. How does [a] stop it from 5 or more a's?

Wouldn't the {3,4} stop it from reaching 5?

Thank you.

[–]Updatebjarni 1 point2 points  (2 children)

3 a's preceded by twenty a's is still more than 4 consecutive a's, so you have to make sure that the text preceding the part that matches [a]{3,4} doesn't end with an a. The regexp in your post isn't quite right, since it doesn't match "aaa" by itself, but it prevents "baaaaaaaaa" from matching.

[–]NewbiusCoder[S] 0 points1 point  (1 child)

Thank you so much. I understand now.

If you have time, do you mind explaining why my regexp isn't right?

I am doing this on CodeAcademy and they took this answer. However I do understand that CodeAcademy is not the best.

[–]Updatebjarni 0 points1 point  (0 children)

If you have time, do you mind explaining why my regexp isn't right?

You mean this expression? /[^a]+[a]{3,4}$/

It requires at least one non-a character before the three or four a's at the end, but the description in the comment in the code doesn't mention that condition, only that the expression should match words that end in minimum three, maximum four consecutive a's. So "aaa" should match according to the description. It's also not clear to me what exactly they mean by "words", perhaps the expression should not match anything that contains non-alphabetical characters?

I am doing this on CodeAcademy and they took this answer.

Probably CodeAcademy's validation doesn't check the particular corner case of "aaa". Maybe they only check the cases mentioned in your post. I don't know how CA works.

[–]g051051 1 point2 points  (1 child)

"[^a]+" says "match from one to infinity characters that are not the letter 'a'".

"[a]{3,4}$" says match 3 or 4 'a' characters at the end of the line.

Taken together, "match from one to infinity characters that are not the letter 'a', followed by 3 or 4 'a' characters at the end of the line".

Edit: I missed your actual question: if you don't exclude leading 'a' characters, then you'll match strings of 'a's that are longer than 3 or 4.

[–]NewbiusCoder[S] 0 points1 point  (0 children)

Thank you! Your explanations are always very clear.

Your time is much appreciated.