Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Regular Expressions in Python Introduction to Regular Expressions Groups

lindseyk
lindseyk
4,506 Points

Optional characters/values

I'm curious as to why Kenneth uses * to make something optional, rather than ?, as in the other cases. I realize the ? finds '1 or none,' and the * finds 'any amount or none.' So I assume the ? would be only used for a single character, whereas the * would be used for an entire group. However, (unless I am just confused), it seems he makes a full phone number optional by using ? rather than *. Can someone explain the decision to use one over the other? Thanks!

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You have the meanings of * and ? correct. However, instead of applying to just a character, ? can be used to make an entire group optional (1 or none) as was done to the phone number.

Using ? guards against poorly formatted input such as an entry with two phone numbers, or if the phone number area code has more than a single set of parentheses. If you know your data really well, then you might get away with using * in place of ?. This issue then becomes readability as the next code reviewer might think that "more than one is OK".

When choosing between ?, *, and +, try saying what you are searching for out loud using natural language. If you say "1 or none", "any number", or "1 or more", then the choice is clear. Otherwise, you might need specific numbers which use the {min, max} notation. {2,} is "two or more", {,3} is "three or less", {4,5} is "four or five".

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi lindseyk,

I found this section sort of confusing as well. However, I think I may have it kind of figured out.

Both the * and the ? can be used as creating an optional condition.

\w* will match zero or more word characters. So if there is none, all's good. But if there is some, it can be any number of them. So... optional or an indeterminate number of them.

\w? will match zero or ONE word character. So if there is none, all's good. But if there can also be one and only one. So... optional or ONLY one character.

So, when deciding which one to use, it will come down to what you are searching for if there is something there to be found.

Hope that helps. Keep Coding! :)

lindseyk
lindseyk
4,506 Points

Thank you both! :) Ok... this is beginning to make more sense to me, I think. So, if I search using ?, I am seeking an optional instance of exactly the format I have typed. If I search using *, this gives more flexibility (which could be beneficial or not, depending), and the results I obtain could be either a) nothing, b) exactly the format I gave, or c) a variation on the format I gave (because it would allow for repetition of the entire thing or individual characters within the thing.) Is that correct?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Almost correct. The ...or individual characters within the thing.) is incorrect. The * or ? only applies to the character or character set or group immediately preceding the * or ? in its entirety.

The "individual character within a thing" would apply if that thing was a set.

r'Hello [a-yA-Y ]*' would match any name of any length, including blank name, as long as there was no z present. `

lindseyk
lindseyk
4,506 Points

Ah, I see! That's crazy, but pretty neat. I just have to remember that a set and a group will respond differently. Ok....

Thanks so much, your explanations are great!