简体   繁体   English

根据重复模式分割字串

[英]Split string based on a recurring pattern

I have the following sting: 我有以下问题:

a='text1fig [1]text2fig [15]text3fig [234]text4fig [2234]text5' 

I want to split it to: 我想将其拆分为:

texts=['text1','text2','text3','text4','text5']

I tried: 我试过了:

import re    
texts=re.split('fig \\[[0-3000]\\]',texts) .  

but it doesn't work. 但这不起作用。
thank you 谢谢

Number ranges don't work like that. 数字范围不是那样的。 Just use \\d instead. 只需使用\\d Additionally, you'll want a single backslash, as a double backslash is taken to be a literal backslash (you want it to escape the [ / ] metachars instead). 另外,您将需要一个反斜杠,因为将双反斜杠视为文字反斜杠(您希望它转义[ / ]元字符)。

text = re.split(r'fig\s*\[\d+\]', a)

print(text)
['text1', 'text2', 'text3', 'text4', 'text5']

Regex Details 正则表达式详细信息

fig   
\s*   # 0 or more whitespace chars 
\[    # literal opening brace
\d+   # 1 or more digits
\]    # literal closing brace

You want to use "re.findall" instead of "re.split" 您要使用“ re.findall”而不是“ re.split”

texts = re.findall('text\d', a)

['text1', 'text2', 'text3', 'text4', 'text5']

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

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