Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression question
#1
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.
Reply
#2
regex101.com to test your regex
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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-')]
Reply
#4
(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?
Reply
#5
(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=
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
[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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular expression help anilrajr 4 302 May-08-2024, 06:18 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 380 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,742 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,730 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,998 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,214 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,562 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,703 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 3,006 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,459 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020