R- PROGRAMMING: Advanced Regex- Lesson 2: CAPTURE GROUPS

Screen Link:

My Code:

pattern  <-  "(?i)(\\w+python\\s|python [\\d])" # calls for flavor and versions of python
python_flavors <- str_match(titles, pattern)[,2]
python_titles_freq <- table(str_to_lower(python_flavors))

What I expected to happen:
I have wanted to see both the flavors and versions in the official solution.

What actually happened: this is the official suggested solution, the code below:

pattern  <-  "(?i)(python [\\d])"
python_flavors  <-  str_match(titles, pattern)[,2]
python_titles_freq  <-  table(str_to_lower(python_flavors))

To my understanding of the problem here, it looks like we’re to program for both the python flavors and the versions. But looking at the official solutions, nothing brings up the python flavors in the titles. Is my pattern acceptable? It does give both the flavors and the versions . Please check it out for me and confirm whether it’s a legal syntax.

Hi @kadenteh, I did run your pattern and the syntax is valid. I think your regex addresses the requirements of the instructions since they asked for python flavors and their versions.

I assume the suggested solution is due to the following line in the instructions:

Any time ‘python’ is mentioned, followed by a space, followed by one digit characters.

However, this does not fit the general requirement:

Create a variable, pattern , containing the regular expression to capture python flavor mentions and their versions using capture groups .

So, your regex may be rejected by the answer checker but it seems to be the appropriate regex in this case.

Good job!

1 Like

Thanks for your prompt response. I’m glad you mentioned that my regex might be the appropriate answer. Thanks once again for your concern for your students.

1 Like

Hi @kadenteh, I am also a learner here at DQ but love to help fellow learners when I have the knowledge to do so. I have worked through that mission but reading your post and going back to it, I realized your solution appropriately addresses the instructions for that particular lesson.

Thanks for your greater attention to detail!

Alright, I see. Thanks.

1 Like