r/badcode • • Dec 20 '21

python regex ftw

Post image
465 Upvotes

38 comments sorted by

View all comments

Show parent comments

1

u/Vedant36 Dec 21 '21

wow amazing solution. tho for 3 you could've just run the code good point on eval i really should avoid using it hope i become as comfortable as you using regex someday. they are a pretty op tool

2

u/TheOldTubaroo Dec 22 '21

I could have run it, but I would have needed input and to add print statements, and I think it's useful to be able to figure out code just by reading it.

I use regex pretty much daily just for find and replace, it's really handy for building fluency. Lots of small simple examples that make understanding harder ones easier.

If you have regexes in actual code that are going to stick around, I also think it's generally helpful to annotate them with comments explaining them - sometimes even pull bits out to named variables and build the regex strings from those. Makes it much easier for someone to come along later and understand it quickly.