Python Forum

Full Version: regular expression question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
regex101.com to test your regex
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-')]
(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?
(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=
(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
[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.