Regex - help please! | SoloLearn: Learn to code for FREE!

+6

Regex - help please!

I'm learning about regular expressions from the book Automate the boring stuff with Python, and there's an exercise that's driving me nuts, my regex works with search but not with findall, I just don't get why (full description in the code): https://code.sololearn.com/c5BqNj90lv36/?ref=app Why the empty list???

2/27/2020 4:37:45 PM

Alina Sansevich

24 Answers

New Answer

+5

Russ I finally found time to read about lookarounds, I will need to do something with them to really get them, but I have a much better understanding now. That's a great website, thank you for sharing!

+4

Russ I think it avoids findall from returning tuples, it's a non capturing group, so it's as if it wasn't a group for findall. Ryan what do you say?

+3

But it works! Thank you for your time! The book's answer doesn't work with text, so you just wrote something better than the author! 👏👏👏 I was still struggling with it 🤯

+2

Your 'num_regex' pattern has special characters that specify the matches must appear from the very start of the string ('^') and at the end of the string ('$'). That's why findall() isn't finding anything at all.

+2

Hahaha, same here, I'll let you know if I succeed 😁👍

+2

I'm not sure if you can call this a 'solution' or not. It's horrific! https://code.sololearn.com/ch5SLTr4NcMS/?ref=app

+2

re.compile(r'^\d{1,3}(,\d{3})*$') That's from the book, it's the same as mine, but I thought it had to work in a text, otherwise what's the point? I also found answers that would match 34,567 but I deleted those. It didn't say it had to be a one liner

+2

Ryan Wow, such a simple solution! Thank you! 👏👏👏

+2

Mirielle👽I respect the author, that was just a joke, I will study the whole book, which I think is great and I'm learning a lot from it. I know that I have a lot to learn, and I never said otherwise. I just think that having some humor among fellow learners helps us all to move forward in our own paths. I really don't understand what's motivating your messages in this thread.

+2

Russ Thank you for your last comment to this thread, for me it's the other way around, lookarounds are confusing for me (and non-capturing groups more straightforward), so I'll read more about those and use your example, I hope to get it this time

+1

Russ Ahhhh, so I misunderstood how those characters work! Thank you!!! I'll try again now, it seemed easier than it is... 😅

+1

Alina Sansevich Good luck! I'm still trying to find a solution that works with findall() myself.

+1

Same!

+1

num_regex = re.compile(r'\b(\d{1,3}(,\d{3})*)\b') found2 = re.findall(num_regex, text) print(*[i[0] for i in found2]) The above nearly worked but it would include 12 and 34,567 from the 12,34,567 and I couldn't figure out a succinct way to filter those out. No worries, I enjoyed the challenge. What was the given answer out of interest? Was it necessary to be a single liner?

+1

After doing more reading, I've managed to come up with this. num_regex = re.compile(r'(?<!\d|,)\d{1,3}(?:,\d{3})*(?!\d|,)') This will return only the correct numbers and in their entirety with findall(). I seem to find lookarounds (the (?<!...] and (?!...) groups) easier to grasp than the concept of non-capturing groups for some reason.

+1

Mirielle👽I respect the author, that was just a joke, I will study the whole book, which I think is great and I'm learning a lot from it. I know that I have a lot to learn, and I never said otherwise. I just think that having some humor among fellow learners helps us all to move forward in our own paths. I really don't understand what's motivating your messages in this thread.

+1

Alina Sansevich In case you are interested, the web page I learned lookarounds from is this: https://www.rexegg.com/regex-disambiguation.html#lookarounds The way it explained them just made a lot if sense to me. Hope it helps you too.

+1

No problem. That's great to hear - thanks for letting me know!

0

Ï know we have to put \ and what is the next character please help

0

Great answer Ryan ! Could you explain how the '?:' works in num_regex = re.compile(r'^\d{1,3}(?:,\d{3})*$') please?