Python 拆分包含数字、罗马数字和项目符号列表的字符串的最佳方法是什么?

Python 拆分包含数字、罗马数字和项目符号列表的字符串的最佳方法是什么?,python,regex,split,Python,Regex,Split,我试图将一个字符串拆分为多个具有不同格式的列表。最好的方法是什么 string = "something here: 1) A i) great ii) awesome 2) B" another_string = "But sometimes it is different (1) yep (2) not the same i. or this ii. another bullet (3.1) getting difficult huh? 3.1.1 okay i'm done" 理想情况

我试图将一个字符串拆分为多个具有不同格式的列表。最好的方法是什么

string = "something here: 1) A i) great ii) awesome 2) B"

another_string = "But sometimes it is different (1) yep (2) not the same i. or this ii. another bullet (3.1) getting difficult huh? 3.1.1 okay i'm done"
理想情况下,我希望能够拆分任何可能的编号或项目符号列表

字符串的所需输出:

something here: 1) A 
i) great 
ii) awesome 
2) B
另一个_字符串的所需输出:

But sometimes it is different (1) yep
(2) not the same
i. or this 
ii. another bullet
(3.1) getting difficult huh?
3.1.1 okay i'm done

您可以将
re.split
与以下正则表达式(借用罗马数字regex)一起使用,以拆分输入字符串,然后使用迭代器将其连接起来:

import re
def split(s):
    i = iter(re.split(r'(\(?\d+(?:\.\d+)+\)?|\(?\d+\)|\(?\b(?=M|(?:CM|CD|D?C)|(?:XC|XL|L?X)|(?:IX|IV|V?I))M{0,4}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})[.)])', s, flags=re.IGNORECASE))
    return next(i) + '\n'.join(map(''.join, zip(i, i)))
因此,通过您的示例输入:

split(string)
将返回:

something here: 1) A 
i) great 
ii) awesome 
2) B
But sometimes it is different (1) yep 
(2) not the same 
i. or this 
ii. another bullet 
(3.1) getting difficult huh? 
3.1.1 okay i'm done
以及:

将返回:

something here: 1) A 
i) great 
ii) awesome 
2) B
But sometimes it is different (1) yep 
(2) not the same 
i. or this 
ii. another bullet 
(3.1) getting difficult huh? 
3.1.1 okay i'm done

你想要的结果是什么?@Ajax1234刚刚修改了我的问题好的,当然,理论上你可以在数字上与正则表达式分开。。。然而,为了使代码更通用,我们如何处理文本可能包含数字的事实?例如:
(3.1)
2.4meters@AntonvBR我想2.4也将作为字符串的另一个子字符串被截断。我不确定还有别的办法。@echan00是的,但是。。。这里的问题是。你想做什么?是否要在输出前验证拆分?可能会构建一个拆分或追加的程序。嗯,那里的大正则表达式(让我头晕),:-),至少它仍然有效:-)这太棒了,但我注意到了一些其他问题。。表示为(a)、(b)、(c)、(a)、(b)、(c)的列表将被删除。像Mr.和Mrs.这样的缩写词也被删掉了。像www.google.com、www.sfc.com.hk这样的网站URL也被删除了。@echan00我明白了。我已经更新了我的答案。请再试一次。我看到一个奇怪的例子,(I)没有拆分,(ii)拆分为“(”和“ii”)。我还看到一个案例,即“(第486章)”和“615章”被拆分,但没有拆分。知道为什么吗?@blhsing不管怎么说,你们都很棒,会把答案标记为已解决