PYTHON REGEXP 用模式本身和替换来替换已识别的模式?

发布于 2025-01-05 01:25:00 字数 515 浏览 1 评论 0原文

文本

-​​.1。这太棒了。2。谷歌刚刚毁了Apple.3。苹果毁了自己! pattern = (dot)(number)(dot)(singlespace)

假设您有 30 到 40 个句子,其中段落编号符合上述模式。

标签应替换为段落编号后面! 使用 re.sub()

我希望文本为:

</p> <p style="text-align: justify;">1. This is just awesome.</p> <p style="text-align: justify;">2. Google just ruined Apple.</p> <p style="text-align: justify;">3. Apple ruined itself!

Text-

.1. This is just awesome.2. Google just ruined Apple.3. Apple ruined itself!
pattern = (dot)(number)(dot)(singlespace)

Imagine you have 30 to 40 sentences with paragraph numbers in the above pattern. A <p> tag should be replaced behind THE PARAGRAPH NUMBER! USING re.sub()

I want the text to be:

</p> <p style="text-align: justify;">1. This is just awesome.</p> <p style="text-align: justify;">2. Google just ruined Apple.</p> <p style="text-align: justify;">3. Apple ruined itself!

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

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

发布评论

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

评论(1

等风来 2025-01-12 01:25:00

这就是正则表达式中匹配组的用途。

你想要的是这样的:

new_string = re.sub(r'\.(\d+\. )', '</p><p style="text-align: justify;">\\1', old_string)

This is what matching groups are for in regular expresssions.

What you want is something like this:

new_string = re.sub(r'\.(\d+\. )', '</p><p style="text-align: justify;">\\1', old_string)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文