+ 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???

27th Feb 2020, 4:37 PM
Alina Sansevich
Alina Sansevich - avatar
24 Respuestas
+ 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!
4th Mar 2020, 1:12 PM
Alina Sansevich
Alina Sansevich - avatar
+ 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?
28th Feb 2020, 1:18 PM
Alina Sansevich
Alina Sansevich - avatar
+ 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 🤯
27th Feb 2020, 6:24 PM
Alina Sansevich
Alina Sansevich - avatar
+ 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 ('
#x27;). That's why findall() isn't finding anything at all.
27th Feb 2020, 5:22 PM
Russ
Russ - avatar
+ 2
Hahaha, same here, I'll let you know if I succeed 😁👍
27th Feb 2020, 5:41 PM
Alina Sansevich
Alina Sansevich - avatar
+ 2
I'm not sure if you can call this a 'solution' or not. It's horrific! https://code.sololearn.com/ch5SLTr4NcMS/?ref=app
27th Feb 2020, 6:13 PM
Russ
Russ - avatar
+ 2
re.compile(r'^\d{1,3}(,\d{3})*
#x27;) 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
27th Feb 2020, 6:38 PM
Alina Sansevich
Alina Sansevich - avatar
+ 2
Ryan Wow, such a simple solution! Thank you! 👏👏👏
27th Feb 2020, 8:44 PM
Alina Sansevich
Alina Sansevich - avatar
+ 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.
1st Mar 2020, 12:41 AM
Alina Sansevich
Alina Sansevich - avatar
+ 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
1st Mar 2020, 9:12 PM
Alina Sansevich
Alina Sansevich - avatar
+ 1
Russ Ahhhh, so I misunderstood how those characters work! Thank you!!! I'll try again now, it seemed easier than it is... 😅
27th Feb 2020, 5:32 PM
Alina Sansevich
Alina Sansevich - avatar
+ 1
Alina Sansevich Good luck! I'm still trying to find a solution that works with findall() myself.
27th Feb 2020, 5:40 PM
Russ
Russ - avatar
+ 1
Same!
27th Feb 2020, 5:42 PM
Russ
Russ - avatar
+ 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?
27th Feb 2020, 6:32 PM
Russ
Russ - avatar
+ 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.
29th Feb 2020, 7:47 PM
Russ
Russ - avatar
+ 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.
1st Mar 2020, 12:41 AM
Alina Sansevich
Alina Sansevich - avatar
+ 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.
1st Mar 2020, 9:23 PM
Russ
Russ - avatar
+ 1
No problem. That's great to hear - thanks for letting me know!
4th Mar 2020, 2:09 PM
Russ
Russ - avatar
0
Ï know we have to put \ and what is the next character please help
28th Feb 2020, 7:46 AM
bheki khumalo
0
Great answer Ryan ! Could you explain how the '?:' works in num_regex = re.compile(r'^\d{1,3}(?:,\d{3})*
#x27;) please?
28th Feb 2020, 12:21 PM
Russ
Russ - avatar