You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python正则表达式:如何匹配除特定标题外的全部内容?

正则解决方案

核心思路

需要精准匹配并移除两类前缀内容:

  • 数字序列+英文点+空格+带冒号的标题(如 2. Shifting Your Mindset:)
  • 小写字母+英文点+空格+带冒号的标题(如 a. Self-Awareness:)

匹配替换正则

使用以下正则表达式执行全局替换操作(将匹配到的内容替换为空字符串):

^\s*(?:\d+\.\s+\w[\w\s]+:|[a-z]\.\s+\w[\w\s]+:)\s*

正则规则拆解:

  • ^\s*:匹配行首任意空白字符(含换行、空格、制表符)
  • (?:...):非捕获分组,同时兼容两种前缀模式
    • \d+\.\s+\w[\w\s]+::匹配数字+点+空格+标题文本+冒号(对应有序列表标题)
    • [a-z]\.\s+\w[\w\s]+::匹配小写字母+点+空格+标题文本+冒号(对应字母前缀标题)
  • \s*:匹配标题冒号后的任意空白字符

操作步骤

  1. 将目标文本导入支持正则替换的编辑器(如VS Code、Sublime Text)或工具
  2. 开启正则匹配模式
  3. 输入上述正则表达式,替换内容设为空
  4. 执行全局替换,即可得到预期结果

效果验证

原文本片段:

  1. Shifting Your Mindset: Transforming your mindset from a negative or limited mindset to a positive one requires conscious effort and practice. Here are some key strategies to help you make that shift
    a. Self-Awareness: Start by becoming aware of your thoughts and inner dialogue. Notice any negative self-talk or limiting beliefs that may be holding you back financially.

替换后:

Transforming your mindset from a negative or limited mindset to a positive one requires conscious effort and practice. Here are some key strategies to help you make that shift
Start by becoming aware of your thoughts and inner dialogue. Notice any negative self-talk or limiting beliefs that may be holding you back financially.

内容的提问来源于stack exchange,提问作者H_coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:17:07