gpt4 book ai didi

python - 如何获取字符串中另一个字符周围的一些字符?

转载 作者:行者123 更新时间:2023-12-01 02:39:47 25 4
gpt4 key购买 nike

我有:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form.

我想搜索1500s,然后选择它周围的一些字符,例如:eversince the 1500s,when anknown。考虑到我正在 for 循环中搜索,试图在一个很长的字符串中找到所有 1500 。因此,下一个循环将查找:自 1500 年代再现以来已使用

我正在使用正则表达式来查找子字符串:

substring = re.findall('1500s', string)

但是如何同时选择它周围的 20 个字符呢?

最佳答案

正如 cricket_007 所说,您可以尝试使用 .{20} 作为正则表达式模式的书挡。

substring = re.findall('.{0,20}1500s.{0,20}', s)

关于python - 如何获取字符串中另一个字符周围的一些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45840799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com