Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Improve your Python regex skills with 75 interactive exercises (github.com/learnbyexample)
175 points by asicsp on Nov 30, 2021 | hide | past | favorite | 12 comments


Hello!

I wrote a GUI app written in `tkinter` to help you practice Python regular expressions. Total 75 questions on `re.search`, `re.sub`, `re.split` and `re.findall` that'll test your understanding of anchors, alternation, grouping, escaping metacharacters, dot metacharacter, quantifiers, character class, grouping, lookarounds, flags, etc.

These exercise questions have been adapted from my "Python re(gex)?" ebook (free to read online [0]). See Exercises.md [1] for all the exercise questions from this book.

I'd appreciate your feedback :)

[0] https://learnbyexample.github.io/py_regular_expressions/

[1] https://github.com/learnbyexample/py_regular_expressions/blo...


I like it! Here is a gotcha my students stumble on. I have not noticed it while skimming your book: ^ and $ do not have special priorities. To match alternatives, write ^(abc|def)$, not ^abc|def$


Yeah, that's a good case to be aware of.

I do mention it in the book while discussing anchors (indicating alternatives have their own independent anchors) [0] and grouping [1].

I also discuss how `re.fullmatch` is useful, especially when constructing an alternation pattern from a list of terms. Easier to use fullmatch instead of manually adding a group and string anchors.

[0] https://learnbyexample.github.io/py_regular_expressions/alte...

[1] https://learnbyexample.github.io/py_regular_expressions/alte...


Without the group, does this match abc at the start or def at the end?


Yes, exactly.


Was looking for a December project... thank you for this.


This is real NSFW content. Luckily I don't have a python interpreter on my machine or my workday would basically be over. I heard you can train discipline by doing more regex puzzles for that matter...

Looks really good. Will test it when I get home.


Cool post! I feel there's a lot of experienced programmers browsing HNews. For someone who has never dabbled with Regex and only basic Python, is this something for complete beginners? Or is it more advanced tricks/catches?


My book (https://github.com/learnbyexample/py_regular_expressions) is for complete newbies to regex, provided they know the basics of Python. At the end of each chapter, there are exercises to test your understanding of concepts presented until that chapter.

These interactive exercises cover about 70% of the exercises from the book, meant for beginner to intermediate level programmers. There are a few tricky exercises, but overall the idea is to help you get comfortable with regex syntax and features.


Thank you very much for both the link and offering it for free at the moment!


Simple & helpful! Thanks for making this..


Simple and cool!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: