简体   繁体   English

正则表达式内的负数后的命名捕获组

[英]named capturing group inside negative lookbehind regex

I want to avoid catching input as: 我想避免捕获输入为:

:):) but want to catch sa:)ds or simply want to exclude from result if there are two or more of the same tags which are touching one another. :) :)但想要捕获sa:)ds或只是想在结果中排​​除两个或多个相同标签彼此接触的情况。

My logic says I need to use negative lookbehind and named capture group but canot make it working and I become unsure if correct way. 我的逻辑说我需要在后面使用否定的lookback和命名捕获组,但是不能使它正常工作,并且我不确定是否正确。

I tried with: (?<!(?P<happy>:\\)))(?P=happy) so if I have input as :):) --:)-abc I want to match only from second line ":)" 我尝试使用(?<!(?P<happy>:\\)))(?P=happy)所以如果我输入为:) :)-)--- abc,我只想从第二行匹配:)“

If you are going to build a regex for .NET don't work with other RegEx engines to test your patterns. 如果要为.NET构建正则表达式,请不要与其他RegEx引擎一起测试您的模式。 That said, you can benefit from variable-length lookbehinds in .NET but not PCRE (engine you're working with). 就是说,您可以从.NET中的变长后置中受益,但不能从PCRE(正在使用的引擎)中受益。

This would be a workaround in .NET: 这是.NET中的解决方法:

(?<happy>:\))(?<!\k<happy>{2,})(?!\k<happy>)

That obviously doesn't work in regex101.com 显然在regex101.com中不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM