这是获得预期结果的一种方法:
import re
a = '1) 3D Technical/Process animations, 2) Explainer videos, 3) Product launch videos'
r = [s for s in map(str.strip,re.split(r',? *[0-9]+(?:\)|\.) ?', a)) if s]
print(*r,sep='\n')
3D Technical/Process animations
Explainer videos
Product launch videos
- 分隔符的模式
r',? *[0-9]+(?:\)|\.) ?' 可以分解如下:
-
,? 一个可选的尾随逗号
-
* 数字前的可选空格(或多个)
-
[0-9]+ 至少一个数字的序列
-
(?:\)|\.) 后跟右括号或句点。开头的 ?: 使其成为非捕获组,因此 re.split 不会将其包含在输出中
-
? 括号或句点后的可选空格(您可能需要删除 ? 或将其替换为 +,具体取决于您的实际数据
re.split 的输出被映射到 str.strip 以删除前导/尾随空格。这是一个列表推导式,它将过滤掉空字符串(例如,在第一个分隔符之前)
如果没有编号的逗号或斜杠也用作分隔符,您可以将其添加到模式中:
def splitItems(a):
pattern = r'/|,|(?:,? *[0-9]+(?:\)|\.) ?)'
return [s for s in map(str.strip,re.split(pattern, a)) if s]
输出:
a = '3D Technical/Process animations, Explainer videos, Product launch videos'
print(*splitItems(a),sep='\n')
3D Technical/Process animations
Explainer videos
Product launch videos
a = '1. Hello 2. Hi'
print(*splitItems(a),sep='\n')
Hello
Hi
a = "Great, what's up?! , Awesome"
print(*splitItems(a),sep='\n')
Great
what's up?!
Awesome
a = '1. Medicines2. Devices 3.Products'
print(*splitItems(a),sep='\n')
Medicines
Devices
Products
a = 'ABC/DEF/FGH'
print(*splitItems(a),sep='\n')
ABC
DEF
FGH
如果您的分隔符是非此即彼模式的列表(意味着只有一个模式始终适用于给定字符串),那么您可以在循环中按优先顺序尝试它们并返回产生多个部分的第一个拆分:
def splitItems(a):
for pattern in ( r'(?:,? *[0-9]+(?:\)|\.) ?)', r',', r'/' ):
result = [*map(str.strip,re.split(pattern, a))]
if len(result)>1: break
return [s for s in result if s]
输出:
# same as all the above and this one:
a = '1. Arrangement of Loans for Listed Corporates and their Group Companies, 2. Investment Services wherein we assist Corporates, Family Offices, Business Owners and Professionals to invest their Surplus Funds to invest in different products such as Stocks, Mutual Funds, Bonds, Fixed Deposit, Gold Bonds,PMS etc 3. Estate Planning'
print(*splitItems(a),sep='\n')
Arrangement of Loans for Listed Corporates and their Group Companies
Investment Services wherein we assist Corporates, Family Offices, Business Owners and Professionals to invest their Surplus Funds to invest in different products such as Stocks, Mutual Funds, Bonds, Fixed Deposit, Gold Bonds,PMS etc
Estate Planning