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

如何编写正则表达式提取两组字符间文本(末组字符可选)

正则提取方案

直接用以下正则表达式即可满足需求,提取结果对应正则的捕获组1:

/tag/([^/]+)(?:/page/.*)?/?$

正则拆解

  • /tag/:精准匹配URL中的固定前缀
  • ([^/]+):核心捕获组,匹配所有非斜杠字符,这就是你要提取的目标内容
  • (?:/page/.*)?:非捕获可选组,匹配可能存在的/page/后续内容,?表示这部分可以没有
  • /?$:匹配末尾可能存在的斜杠,避免结尾斜杠干扰匹配

匹配验证

对应你提供的每个URL,捕获结果如下:

  • https://www.myurl.com/tag/important-info-1/page/34/ → important-info-1
  • https://www.myurl.com/tag/otherimportant-info-3 → otherimportant-info-3
  • https://www.myurl.com/tag/moreimportantinfo/page/1 → moreimportantinfo
  • https://www.myurl.com/tag/otherimportant-info-4/ → otherimportant-info-4

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:57:01