Strange Regex Capture Group behavior returning "or" pipes into result

时间:2016-02-12 19:36:48

标签: regex regex-group

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:

  • 0=\0
  • 1=\1
  • 2=\2

however the output of the capture groups I test return the "|" into the result with it seems some redundancy

Output:

  • 0=Meteorologic
  • 1=Meteorologic
  • 2=Meteoro0=|Meteorological
  • 1=|Meteorological
  • 2=|Meteoro

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.

2 个答案:

答案 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)?))

RegEx Demo