简体   繁体   English

正则表达式与NOT选项匹配C#

[英]Regex with NOT option match c#

I have the following text input- 我有以下文字输入-

Host             Expiration  Features            
**172.17.64.120    never**       CPSM-C-BASE CPSB-NPM CPSB-EPM CPSB-LOGS CPSB-MNTR CPSB-MPTL CPSB-UDIR CPSB-PRVS CPSB-GBLP CK-77B458DF8D52
**172.17.64.120    never**       CPSM-C-U CPSB-NPM CPSB-EPM CPSB-LOGS CPSB-MNTR CPSB-MPTL CPSB-UDIR CPSB-PRVS CPSB-GBLP CK-FF18475CE402
**10.10.5.17       10May2018**   CPSM-C-U CPSB-NPM CPSB-EPM CPSB-LOGS CPSB-MNTR 

(some other text..)

LICENSE **10.10.5.17 10May2018** 
LICENSE **172.17.64.120 never** 
LICENSE **172.17.64.120 never** 

I want to get all the 3 licenses without duplication - The following regex pattern - \\d+\\.\\d+\\.\\d+\\.\\d+\\s*(\\d{1,2}[a-zA-Z]{3}\\d{4} |never) matches these results: 我想获得所有3个许可证而不重复-以下正则表达式模式- \\d+\\.\\d+\\.\\d+\\.\\d+\\s*(\\d{1,2}[a-zA-Z]{3}\\d{4} |从不匹配以下结果:

172.17.64.120    never
172.17.64.120    never
10.10.5.17       10May2018
10.10.5.17 10May2018
172.17.64.120 never
172.17.64.120 never

The issue is, that I want to get the results without duplications. 问题是,我想获得没有重复的结果。 Is there anyway to insert additional not option to the regex that will not match the last 3 result (those who start with LICENSE+spaces) 无论如何,是否要在正则表达式中插入不符合最后3个结果(以LICENSE +空格开头的那些)的其他not选项。

10.10.5.17 10May2018
172.17.64.120 never
172.17.64.120 never

Thanks :) 谢谢 :)

If i understand what you are saying... If your regex is working, then don't complicated it, just use a Distinct on the results 如果我明白您在说什么...如果您的正则表达式正常工作,请不要使其复杂化,只需对结果使用Distinct

List<string> results = <put your matches in here>;

results = results.Distinct();

Enumerable.Distinct Method (IEnumerable) Enumerable.Distinct方法(IEnumerable)

Returns distinct elements from a sequence by using the default equality comparer to compare values. 通过使用默认的相等比较器比较值,从序列中返回不同的元素。

You may prepend your pattern with a word boundary and a negative lookbehind that will fail the matches after LICENSE + one or more whitespaces: 您可以在模式前加上单词边界和负向后看,这将在LICENSE +一个或多个空格之后使匹配失败:

\b(?<!\bLICENSE\s+)(\d{1,3}(?:\.\d{1,3}){3})\s*(\d{1,2}[a-zA-Z]{3}\d{4}|never)\b

See the regex demo . 参见regex演示

Details 细节

  • \\b - a word boundary \\b单词边界
  • (?<!\\bLICENSE\\s+) - immediately to the left of the current location, there can't be LICENSE followed with 1+ whitespace chars (?<!\\bLICENSE\\s+) -当前位置左侧,不能有LICENSE后跟1+个空格字符
  • (\\d{1,3}(?:\\.\\d{1,3}){3}) - Group 1: one, two or three digits, and then 3 occurrences of a dot followed with 1 to 3 digits (\\d{1,3}(?:\\.\\d{1,3}){3}) -组1:一位,两位或三位数字,然后出现3个点,后跟1至3位数字
  • \\s* - 0+ whitespaces \\s* -0+空格
  • (\\d{1,2}[a-zA-Z]{3}\\d{4}|never) - 1 or 2 digits, 3 ASCII letters and 4 digits, or a whole word never (\\d{1,2}[a-zA-Z]{3}\\d{4}|never) -1或2位数字,3个ASCII字母和4位数字,或者整个单词never
  • \\b - word boundary. \\b单词边界。

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

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