简体   繁体   English

匹配字符串之间的正则表达式

[英]Regex between match string

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. ?是否可以只取数字 1. 和 2. 之间的任何元素?

output = [[Ab2 C34], [Dog Cat4]]

Use re.findall :使用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 .使用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']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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