使用正则表达式将列表项拆分为两个项目

发布于 2024-12-28 08:37:24 字数 474 浏览 3 评论 0原文

所以我输入了一个包含如下元素的列表:

“2008年建筑重建重铺和施工日历年” 街道和街道的修复道路”

,我正在迭代列表。对于每次迭代,首先我想验证模式 INTspacespacefor 是否在字符串中,我知道我可以这样做:

re.search('\\d+\s\sfor',string)

一旦我确认了其中一个元素的匹配,我想将此元素拆分为 INT 和“for”之间的两个元素,有没有一种简单的方法可以做到这一点

[item1,item2,...item10,"calendar year 2008","for construction reconstruction resurfacing & repair of streets & roads",item11,item12...]

So I have input of a list with elements like this:

"calendar year 2008 for construction reconstruction resurfacing &
repair of streets & roads"

and I'm iterating through the list. For each iteration, first I want to verify that the pattern INTspacespacefor is in the string, which I know I can do with:

re.search('\\d+\s\sfor',string)

Once I've confirmed a match on one of the elements, I want to split this element into two elements in place between the INT and the "for". Is there an easy way to do this?

So a sample output for this would be:

[item1,item2,...item10,"calendar year 2008","for construction reconstruction resurfacing & repair of streets & roads",item11,item12...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2025-01-04 08:37:24

您可以使用后视和前视

re.split('(?<=\d)\s\s(?=for)',string)

You can use look behind and look ahead

re.split('(?<=\d)\s\s(?=for)',string)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文