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

PCRE正则如何捕获单词前半段并排除末尾固定后缀?

PCRE正则解决方案

可用正则表达式

基础适配版本(匹配后缀Controller前的内容):

\.controller\.(?P<Controller>\w+)(?=Controller)

高兼容版本(锚定末尾,适配类名中间含Controller的场景):

\.controller\.(?P<Controller>\w+)(?=Controller$)

实现逻辑说明

  • 核心使用PCRE支持的正向前瞻零宽断言(?=Controller),该断言不会消耗匹配字符,仅校验当前位置后方是否符合断言规则
  • 末尾加$的版本会锚定字符串末尾,仅排除末尾固定的Controller后缀,不会影响中间内容的匹配,适配更多业务场景

效果验证

针对你提供的测试文本.controller.CustomerDemographicsController,两个版本的正则都可以在命名捕获组Controller中得到预期结果CustomerDemographics。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 01:18:01