Python Forum
Python regex to get only numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python regex to get only numbers
#1
I have the following script

import re

w = 'KTPA 081653Z 00000KT 10SM BKN022TCU BKN040 OVC001RMK 28/23 A2990 FEW070 RMK AO2 SLP125 TCU SCT080 NW-NE T02830228'
k = re.findall("FEW\d+|SCT\d+|BKN\d+|OVC\d+", w)
print(k)
When I run it, I'm getting
Quote:['BKN022', 'BKN040', 'OVC001', 'FEW070', 'SCT080']
, but I only want the integers from it so that it looks like this
Quote:['022', '040', '001', '070', '080']
. Once I get the integers, I can sort it to get the lowest number.

How would I do that?
Reply
#2
You'll want something more like this:

regex = re.compile("(?:FEW|BKN|OVC|SCT)(\d{3})")
The digits will be in a capture group for each match. Review the Python documentation for regex groups to access them.
Reply
#3
I tried
re.compile("(?:FEW|BKN|OVC|SCT)(\d{3})")
and I was getting
Quote:re.compile('(?:FEW|BKN|OVC|SCT)(\\d{3})')

Then I tried
re.findall("(FEW|SCT|BKN|OVC)(\d{3})", w)
and now I'm getting
Quote:[('BKN', '022'), ('BKN', '040'), ('OVC', '001'), ('FEW', '070'), ('SCT', '080')]

How can I get only the integers from this?
Reply
#4
(Oct-09-2019, 06:57 PM)tantony Wrote: How can I get only the integers from this?
import re

w = 'KTPA 081653Z 00000KT 10SM BKN022TCU BKN040 OVC001RMK 28/23 A2990 FEW070 RMK AO2 SLP125 TCU SCT080 NW-NE T02830228'
k = re.findall("(FEW|SCT|BKN|OVC)(\d{3})", w)
lst = [int(i[1]) for i in k]
print(lst)
Output:
[22, 40, 1, 70, 80]
Reply
#5
@snippsat, thanks that worked. So just to make sure, there's no way to get just the integers from my original regex?
k = re.findall("FEW\d+|SCT\d+|BKN\d+|OVC\d+", w)
With my original regex, I was getting
Quote:['BKN022', 'BKN040', 'OVC001', 'FEW070', 'SCT080']
Reply
#6
The "?:" in the capture group changes it to a non-capturing group. So "(?:FEW|SCT|BKN|OVC)(\d{3})" would result in only the numbers. From what you posted, it looks like you compiled the regex using my code but didn't use it to match anything.
Reply
#7
(Oct-09-2019, 07:45 PM)tantony Wrote: @snippsat, thanks that worked. So just to make sure, there's no way to get just the integers from my original regex?
k = re.findall("FEW\d+|SCT\d+|BKN\d+|OVC\d+", w)
With my original regex, I was getting
Quote:['BKN022', 'BKN040', 'OVC001', 'FEW070', 'SCT080']

(Oct-09-2019, 10:27 PM)stullis Wrote: The "?:" in the capture group changes it to a non-capturing group. So "(?:FEW|SCT|BKN|OVC)(\d{3})" would result in only the numbers. From what you posted, it looks like you compiled the regex using my code but didn't use it to match anything.

Hi!

I think that sometimes, newbies like myself, don't get straightaway what the experienced programmers here unselfishly and kindly provide as answers and advice.

Maybe, if you are also a newbie, you didn't realize that Stullis was also pointing you out another solution, although you had to do the necessary adjustments. Here I'll show you what I think he meant (regex1), comparing it with what you had before (k):

import re
 
w = 'KTPA 081653Z 00000KT 10SM BKN022TCU BKN040 OVC001RMK 28/23 A2990 FEW070 RMK AO2 SLP125 TCU SCT080 NW-NE T02830228'
k = re.findall("FEW\d+|SCT\d+|BKN\d+|OVC\d+", w)
regex1 = re.findall("(?:FEW|BKN|OVC|SCT)(\d{3})", w)
print(k)
print(regex1)
and that produces the following output:
Output:
['BKN022', 'BKN040', 'OVC001', 'FEW070', 'SCT080'] ['022', '040', '001', '070', '080']
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Regex quest 2 2,229 Sep-22-2022, 03:15 AM
Last Post: quest
  Find numbers using Regex giddyhead 18 3,012 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  python regex: get rid of double dot wardancer84 4 2,309 Sep-09-2021, 03:03 PM
Last Post: wardancer84
  Using Regex Expression With Isin in Python eddywinch82 0 2,253 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  Exception handling in regex using python ShruthiLS 1 2,330 May-04-2020, 08:12 AM
Last Post: anbu23
  Python the regex not getting any attributes sarath_unrelax 1 1,824 Dec-19-2019, 11:06 AM
Last Post: Larz60+
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,663 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

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