I am trying to make a capture group to find/replace suffixes.
Please see the example here:
https://www.myregextester.com/?r=b23e74dc
and my summary below:
Regex:
(\b(.*?)(logical|logic))
Requested Ouput:
however the output of the capture groups I test return the "|" into the result with it seems some redundancy
Output:
Clearly I have introduced some error into the regex since I do NOT want the "|" in the output but I am unclear what it could be.
答案 0 :(得分:1)
你应该把它放好,这样你才能看到它。
Meteorologic|Meteorological
0=Meteorologic
1=Meteorologic
2=Meteoro
0=|Meteorological
1=|Meteorological
2=|Meteoro
第一场比赛结束Meteorologic
|气象
第二场比赛开始Meteorologic |Meteorological
在c
和|
之间是单词边界,因此\b
得到满足。
然后.*?
(捕获组2)将|Meteoro
与逻辑匹配。
然后在捕获组3中logical
。
所以,它可以满足您的要求。
修复
要偏向单词边界以找到右边的单词,只需引入一个单词
像这样(\b(?=\w)(.*?)(logical|logic))
关于单词边界的说明:它们是灵巧的。
相当于
(?:
(?:
^
| (?<= [^a-zA-Z0-9_] )
)
(?= [a-zA-Z0-9_] )
|
(?<= [a-zA-Z0-9_] )
(?:
$
| (?= [^a-zA-Z0-9_] )
)
)
希望这有帮助。
答案 1 :(得分:0)
You can use negated pattern [^|]*
to match anything but pipe:
(\b([^|]*)(logic(?:al)?))
Alternatively you can use \w
as well:
(\b(\w*)(logic(?:al)?))