我试图在PCRE中编写一个regex表达式,该表达式捕获单词的第一部分并排除第二部分。第一部分需要根据事务的起始位置容纳不同的值。下面是一个例子:
原始文本:
.controller.CustomerDemographicsController
Regex Pattern attempt:
.controller.(?P<Controller>w+)
结果尝试实现(在的是我想保存在命名的捕获组中的唯一内容):
.合肥。CustomerDemographics控制器
注意:我已经尝试排除使用^
、向后看和向前看。
感谢您的帮助。
可以匹配Controller组中的单词字符,直到最后一个大写字母:
.controller.(?P<Controller>w+)(?=p{Lu})
参见regex演示。细节:
.controller.
- a.controller.
string(?P<Controller>w+)
-命名捕获组"Controller":一个或多个字字符尽可能多(?=p{Lu})
-下一个字符必须是大写字母
注意,(?=p{Lu})
使w+
停止在最后一个大写字母之前,因为w+
模式由于+
量词而贪婪。
同时使用
.controller.(?P<Controller>[A-Za-z]+)[A-Z]
看到证据。
:
--------------------------------------------------------------------------------
. '.'
--------------------------------------------------------------------------------
controller 'controller'
--------------------------------------------------------------------------------
. '.'
--------------------------------------------------------------------------------
(?P<Controller> group and capture to Controller:
--------------------------------------------------------------------------------
[A-Za-z]+ any character of: 'A' to 'Z', 'a' to 'z'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of Controller group
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'