Advanced Regex Expression - Step 9

My Code:
pattern=r"(.+)://([\w.-]+)/?(.*)"
test_url_parts=test_urls.str.extract(pattern, flags=re.I)
url_parts=hn[‘url’].str.extract(pattern, flags=re.I)

Why doesn’t it give the right answer? The first capture group - Must I type (https?)?

Thanks.

Hi @yi.jun.smile26 and welcome to the community!

I remember doing this mission before but I can’t quite remember all the details. Can you please provide a link to the mission screen so that it’s easier to help you? You may want to read this post for tips and tricks for asking technical questions in the community.

One thing I do remember about those missions (and regex in general) is that it’s possible to write an expression that works on the test data but not necessarily on the real data. Also, the system answer checker can only test your answer in so many ways and can sometimes flag a “correct” answer as not acceptable. Regex is tricky like that!

Thanks @mathmike314 ! I shall check out the post.
Here is the link - Learn data science with Python and R projects

Regex is indeed tricky. I’m still trying to digest.

Although it appears you may get the same results, using (.+) as your first capture group as opposed to (https?) could potentially capture things that aren’t a protocol since it so very general. It’s best to design your patterns so they work like “spotlights” to find the text you’re looking for rather than using a “floodlight” approach that’s more broad.

Thanks for the advice @mathmike314 ! Well noted…