Posts: 12
Threads: 4
Joined: Aug 2018
hey there, im a newbie who just started learning python, and i had a problem here:
import re
a = re.compile(r'(\d\d\d-){1,2}?\d\d\d\d')
print(a.findall('111-222-1112, 111-333-2222, 155-2333')) so as u guys can probably see, i want the code to be able to identify both \d\d\d-\d\d\d\d and \d\d\d-\d\d\d-\d\d\d\d , but something went wrong, and the program ends up returning ['222-', '333-', '155-']
i cant really find y its wrong. ik how to write it so that it does what its supposed to do but im just curious of the mistake ive made that i cannot find.
Posts: 8,168
Threads: 160
Joined: Sep 2016
regex101.com to test your regex
Posts: 2,342
Threads: 62
Joined: Sep 2016
Does adding an extra pair of parens help?
a = re.compile(r'((\d\d\d-){1,2}?\d\d\d\d)') Output: [('111-222-1112', '222-'), ('111-333-2222', '333-'), ('155-2333', '155-')]
Posts: 12
Threads: 4
Joined: Aug 2018
Aug-04-2018, 04:52 PM
(This post was last modified: Aug-04-2018, 04:58 PM by Sanlus.)
(Aug-04-2018, 04:47 PM)buran Wrote: regex101.com to test your regex
the website just told me that nothing matches. but in the actual program i still got 3 matches, tho not the ones i want
(Aug-04-2018, 04:50 PM)micseydel Wrote: Does adding an extra pair of parens help?
a = re.compile(r'((\d\d\d-){1,2}?\d\d\d\d)') Output: [('111-222-1112', '222-'), ('111-333-2222', '333-'), ('155-2333', '155-')]
i c what u did, this should alter the whole thing to group 1 and change the \d\d\d to group 2. but isnt the code supposed to return a list of group 0s? or did i mistaken something and theres actually another way to do so?
Posts: 2,342
Threads: 62
Joined: Sep 2016
(Aug-04-2018, 04:52 PM)Sanlus Wrote: i c what u did, this should alter the whole thing to group 1 and change the \d\d\d to group 2. but isnt the code supposed to return a list of group 0s? or did i mistaken something and theres actually another way to do so? I'm a regex amateur, I thought it might help even though it's not perfect, sorry I can't be more helpful than that D=
Posts: 8,168
Threads: 160
Joined: Sep 2016
Aug-04-2018, 06:12 PM
(This post was last modified: Aug-04-2018, 06:12 PM by buran.)
(Aug-04-2018, 04:52 PM)Sanlus Wrote: the website just told me that nothing matches. but in the actual program i still got 3 matches, tho not the ones i want that is not true - https://regex101.com/r/A5Cssw/1
your original regex has 3 pairs of Full match and Group1 match
import re
my_regex = re.compile(r'(\d\d\d-){1,2}?\d\d\d\d')
for match in my_regex.finditer('111-222-1112, 111-333-2222, 155-2333'):
print(match.group(0)) Output: 111-222-1112
111-333-2222
155-2333
Posts: 566
Threads: 10
Joined: Apr 2017
Aug-04-2018, 06:49 PM
(This post was last modified: Aug-04-2018, 06:49 PM by volcano63.)
[EDITED]
By default, RE pattern at https://regex101.com/ is set to PHP, you have to switch it to Python.
Your expression should be changed to (?:\d{3}-){1,2}\d{4} , first group should be changed to non-capturing, and ? in the middle will make it match any 4-digit combination in the text - I presume that is not your intention
Output: In [73]: re.findall(r'(?:\d{3}-){1,2}\d{4}', '''111-222-1112
...: 111-333-2222
...: 155-2333''')
Out[73]: ['111-222-1112', '111-333-2222', '155-2333']
@buran, re.findall matches sub-groups as nested tuples, making non-capturing group forces it to find the whole expression.
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|