边界之间的多重匹配

2024-04-23 05:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个字符串中进行多重匹配,但我只需要找到一些边界之间的匹配

假设我在字符串中有以下字符串和标志。我只需要找到介于HELLO和BYE标志之间的“123.12

Lorem Ipsum是*123.12*印刷和*123.12*行业的虚拟文本*123.12*Ipsum是16世纪业界的*123.12*虚拟文本,当时一个未知的*123.12*取了*123.12*的一个厨房,并将其置乱为*123.12*一个样本*123.12*。您好,*123.12*不仅经历了*123.12*个世纪,*123.12*还跨越了*123.12*排版,保持了*123.12*不变。再见,在*123.12*1960年代,随着包含Lorem Ipsum段落的Letraset表单的发布,它是*123.12*,最近,随着桌面出版软件*123.12*Aldus PageMaker*123.12*版本*123.12*Ipsum的发布

This is an example of what I'm doing

现在我正在对所有具有我需要的特征的子字符串进行匹配,但它在我需要的范围之外进行匹配

PS:引擎是python,不能添加任何库


Tags: 字符串文本hello标志厨房经历边界段落
1条回答
网友
1楼 · 发布于 2024-04-23 05:30:20
string = """Lorem Ipsum is *123.12* dummy text of the printing and *123.12* industry. *123.12* Ipsum has been the industry's *123.12* dummy text ever *123.12* the 1500s, when an unknown *123.12* took a galley of *123.12* and scrambled it to *123.12* a type specimen *123.12*. HELLO *123.12* has survived not only *123.12* centuries, *123.12* also the leap into *123.12* typesetting, remaining *123.12* unchanged. BYE It was *123.12* in *123.12* 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software *123.12* Aldus PageMaker *123.12* versions of *123.12* Ipsum."""

startFlag = 'HELLO'
endFlag = 'BYE'

lookingFor = '*123.12*'

EndOfStartFlagIndex = string.index(startFlag) + len(startFlag)

betweenFlags = string.split(startFlag)[1].split(endFlag)[0]

lookingForInices = [i+EndOfStartFlagIndex for i in range(0,len(betweenFlags)) if betweenFlags[i:i+len(lookingFor)] == lookingFor]

print(lookingForInices)

相关问题 更多 >