简体   繁体   中英

Regex: Numbers between special characters (but not all number)

I'm trying to solve a relatively simple parsing issue that regex seems like it would be great for. I'm still trying to wrap my head around the notation, so I was hoping to get a nudge in the right direction for what I'm trying to do. The string I have is in this format:

x = 'Testing - 12:34: I dont want this number at the end 4567:'

From what I've been able to write:

test = re.findall(r'\b(\d+)\b',x)

will give the output of

['12', '34', '4567']

It's close, but not quite there. The problem is not every string I'm going to be scanning at the end, so I'd like to have a regex statement rather than just

test = test[:2]

Essentially, the condition I'm trying to articulate is 'take the numbers between the - and second :, but nothing else.' (ie 12:34 in the form ['12','34]). Is this possible? Thank you!

http://regexstorm.net/tester?p=%3b%5cd%2b%3d%5cd%2b%5c%3f&i=0014%3b5010730101000033347%3d4510120173%3fAA

This might be a lot easier with two expressions:

import re

x = 'Testing - 12:34: I dont want this number at the end 4567:'

rx_outer = re.compile(r'-((?:[^:]*:){2})')
rx_inner = re.compile(r'\d+')

numbers = [number.group(0) 
            for match in rx_outer.finditer(x) 
            for number in rx_inner.finditer(match.group(0))]

print(numbers)
# ['12', '34']

The "outer" regex defines the slice between the first dash and the second colon while the "inner" regex scans for digits. The logic is wrapped in a list comprehension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM