Hey everyone I have a question. I am new with regex and I found it a little bit confusing about the match. Let say I want to take the element between a pattern of number.
['1.Ab2 C34 2.kj4 nsb', '1.Dog Cat4 2.Bird6 Trex5']
Is it possible to just take whatever the element is between the number 1. and 2. ?
output = [[Ab2 C34], [Dog Cat4]]
Use re.findall
:
inp = ['1.Ab2 C34 2.kj4 nsb', '1.Dog Cat4 2.Bird6 Trex5']
matches = [re.findall(r'\b1\.\s*(.*?)\s*2\.', x)[0] for x in inp]
print(matches) # ['Ab2 C34', 'Dog Cat4']
Try something like this using search
.
import re
new = [[re.search('1.(.*)2.', s).group(1)] for s in ['1.Ab2 C34 2.kj4 nsb', '1.Dog Cat4 2.Bird6 Trex5']]
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.