FAQ: Introduction to Regular Expressions - Quantifiers - Optional

This community-built FAQ covers the “Quantifiers - Optional” exercise from the lesson “Introduction to Regular Expressions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Introduction to Regular Expressions
Natural Language Processing
Practical Data Cleaning

FAQs on the exercise Quantifiers - Optional

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The Learn section states:
The regex The monkey ate a (rotten)? banana will completely match both The monkey ate a rotten banana and The monkey ate a banana .

Why does it match The monkey ate a banana instead of The monkey ate a banana with two whitespaces? Wouldn’t the ? symbol only match the text in the parentheses (rotten)? leaving a white space to the right and left?

EDIT: I am now noticing that Codecademy will not format two white spaces in a row, and will instead display it as one. So is the above issue a visual error or does regex actually only match one whitespace?


Hmm. I think you’re right that there would be two whitespaces in the original search string should the group not match: r'The monkey ate a__banana which would therefore not match ‘the monkey ate a banana’.

No idea if the original technically had two spaces and some html fooey (or whatever it is that ignores multiple spaces) ignored it, but it’s certainly not clear to users. I think this should be raised as an error, ta.


Hey there @czjlee! :grinning:

Very good catch there, thank you for posting this!

I just reported it to Codecademy and will let you know when I hear back.

1 Like

The text has been changed to:
The monkey ate a (rotten )?banana

So as to match both expressions

Thanks again for reporting this :slightly_smiling_face:

1 Like

How does this make sense? The lesson very clearly states that regex ranges are greedy and will only return a match for the largest possible quantity, but then in the exercise, it returns matches for multiple quantities in the range.