Python Forum
Help with a regular expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a regular expression
#1
Hi guys, 
Hoping somebody can help me here. I have a list of email messages and their UID's. I need to use re.findall() to select just the UID from the list. 

The format of the input is:
b' +OK 1 19
b' +OK 2 22
b' +OK 3 25
etc.
Now, I can use this re to return the below format, 
messageUID = re.findall(r'\d+ \d+',messageUID,0)[0]
{msgNum UID}:
1 19
2 22
3 25
4 28
5 35
However, I'm trying to retrieve just the UID (19, 22, 25, 28... etc)
Which is the 2nd occurrence of r' \d+'

​​​​​​​How can I do this?
Reply
#2
First of all, if your input is as regular as you show, I would not use the regex and just split() it.

uid = messageUID.split()[-1]
But if it's useful for other reasons, use parenthesis to change the second digit block into a group.  Then the 0th element will refer to that group.

>>> re.findall(r'\d+ \d+',' +OK 1 19',0)[0]
'1 19'
>>> re.findall(r'\d+ (\d+)',' +OK 1 19',0)[0]
'19'
Reply
#3
(Oct-13-2020, 03:55 PM)bowlofred Wrote: First of all, if your input is as regular as you show, I would not use the regex and just split() it.

uid = messageUID.split()[-1]
But if it's useful for other reasons, use parenthesis to change the second digit block into a group.  Then the 0th element will refer to that group.

>>> re.findall(r'\d+ \d+',' +OK 1 19',0)[0]
'1 19'
>>> re.findall(r'\d+ (\d+)',' +OK 1 19',0)[0]
'19'
Thanks for the suggestion. 
I just found that selecting the second item from the list like this, also gives me what I want. I will try your suggestions if I continue to run into trouble!
 
messageUID=re.findall(r'\d+',messageUID,0)[1]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 331 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,661 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,662 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,920 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,165 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,476 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,671 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,926 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,414 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,491 Jan-14-2021, 11:49 AM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

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