普通EX pression匹配所有有效格式IPv6地址有效、普通、地址、格式

2023-09-08 09:24:41 作者:玛丽莲、猛撸

乍一看,我承认,这个问题看起来像这个问题重复和任何其他与之相关的:

正EX pression匹配有效的IPv6地址

其实这个问题有答案,几乎回答我的问题,,但并不完全。

这是这个问题,我有问题,但过的最成功,在code如下图所示:

 私人字符串RemoveIPv6(字符串sInput)
{
    字符串模式= @"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
    //这是一长串的正则表达式!来源:http://stackoverflow.com/a/17871737/3472690
    //如果(ISCOM pressedIPv6(sInput))
      // sInput = Uncom pressIPv6(sInput);
    字符串输出= Regex.Replace(sInput,模式,);
    如果(output.Contains(地址))
        输出= output.Substring(0,地址:.Length);

    返回输出;
}
 

我曾与正则表达式模式在这个答案,大卫·M·Syzdek的提供的问题答,就是它不匹配和删除IPv6地址,我要把它的完整形式。

我用正则表达式模式主要是取代在空白或空值的字符串IPv6地址。

例如,

 地址:2404:6800:4003:C02 :: 8A
 
IPV6为何难以普及 不能使用那就选择关闭协议吧

除了...

 地址:2404:6800:4003:804 :: 200E
 

最后...

 地址:2001:4998:C:A06 :: 2:4008
 

所有要么没有得到完全的正则表达式匹配,还是没能完全匹配。

如下所示的正则表达式将返回我的串的剩余部分:

 地址:8A

    地址:200E

    地址:2:4008
 

如可以看到的,它已经离开了IPv6地址,这是难以检测和清除,残余由于不同格式的残余承担。下面是本身获得更好的分析,正则表达式:

(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))

因此​​,我的问题是,,这怎么能正则表达式模式进行修正,因此它可以匹配,因此允许彻底清除所有的IPv6地址,从一个不完全包含IPv6的一个字符串地址(ES)本身?

或者,又怎能code段我上面提供予以纠正,以提供所需的结果?

对于那些谁可能想知道,我正在从NSLOOKUP命令的standardOutput和IPv6地址字符串将始终有所不同。对于上面的例子,我那些IPv6地址从google.com和yahoo.com的

我不使用内置的功能来解决一个很好的理由,我不认为将此事暂时DNS条目,因此我使用NSLOOKUP。的

至于code表示是调用该函数,如果需要的话,是如下:(它本身也是另一种功能/方法,或者更确切地说,第一部分的)的

 字符串输出=;
字符串garbagecan =;
字符串统$ P $垫=;
字符串lastRead =;
使用(StreamReader的读卡器= nslookup.StandardOutput)
{
     而(reader.Peek()!=  -  1)
     {
         如果(LinesRead→3)
         {
             TEM $ P $垫= reader.ReadLine();
             TEM $ P $垫= RemoveIPv6(TEM $ P $垫);

             如果(TEM pread.Contains(地址))
                 输出+ = TEM $ P $垫;
             否则,如果(lastRead.Contains(地址))
                 输出+ = TEM pread.Trim()+ Environment.NewLine;
             其他
                 输出+ = TEM $ P $垫+ Environment.NewLine;
             lastRead = TEM $ P $垫;
         }
         其他
             garbagecan = reader.ReadLine();
         LinesRead ++;
     }
 }
 返回输出;
 

校正后的正则表达式应仅允许删除IPv6地址,并留下IPv4地址不变。 将被传递给正则表达式将不包含IPv6地址(ES)本身,而且几乎总是包含其他细节,正因为如此,它是联合国predictable在该指数将地址出现的字符串。 的正则表达式也跳过第一发生的历史之后IPv6,以及由于某些原因所有其他IPv6地址,应该指出的

道歉,如果有任何遗漏的细节,我会尽我所能,包括他们提醒时研究。我也preFER工作code样品,如果可能的话,因为我几乎为零关于正则表达式的知识。

解决方案

(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)

使用 lookarounds 则可以执行一个完整的比赛,而不是一个部分 match.See演示。

https://regex101.com/r/cT0hV4/5

At first glance, I concede that this question looks like a duplicate of this question and any other related to it:

Regular expression that matches valid IPv6 addresses

That question in fact has an answer that nearly answers my question, but not fully.

The code from that question which I have issues with, yet had the most success with, is as shown below:

private string RemoveIPv6(string sInput)
{
    string pattern = @"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
    //That is one looooong regex! From: http://stackoverflow.com/a/17871737/3472690
    //if (IsCompressedIPv6(sInput))
      //  sInput = UncompressIPv6(sInput);
    string output = Regex.Replace(sInput, pattern, "");
    if (output.Contains("Addresses"))
        output = output.Substring(0, "Addresses: ".Length);

    return output;
}

The issues I had with the regex pattern as provided in this answer, David M. Syzdek's Answer, is that it doesn't match and remove the full form of the IPv6 addresses I'm throwing at it.

I'm using the regex pattern to mainly replace IPv6 addresses in strings with blanks or null value.

For instance,

    Addresses:  2404:6800:4003:c02::8a

As well as...

    Addresses:  2404:6800:4003:804::200e

And finally...

    Addresses:  2001:4998:c:a06::2:4008

All either don't get fully matched by the regex, or failed to be completely matched.

The regex will return me the remaining parts of the string as shown below:

    Addresses:  8a

    Addresses:  200e

    Addresses:  2:4008

As can be seen, it has left remnants of the IPv6 addresses, which is hard to detect and remove, due to the varying formats that the remnants take on. Below is the regex pattern by itself for better analysis:

(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))

Therefore, my question is, how can this regex pattern be corrected so it can match, and therefore allow the complete removal of any IPv6 addresses, from a string that doesn't solely contain the IPv6 address(es) itself?

Alternatively, how can the code snippet I provided above be corrected to provide the required outcome?

For those who may be wondering, I am getting the string from the StandardOutput of nslookup commands, and the IPv6 addresses will always differ. For the examples above, I got those IPv6 addresses from "google.com" and "yahoo.com".

I am not using the built-in function to resolve DNS entries for a good reason, which I don't think will matter for the moment, therefore I am using nslookup.

As for the code that is calling that function, if required, is as below: (It itself is also another function/method, or rather part of one)

string output = "";
string garbagecan = "";
string tempRead = "";
string lastRead = "";
using (StreamReader reader = nslookup.StandardOutput)
{
     while (reader.Peek() != -1)
     {
         if (LinesRead > 3)
         {
             tempRead = reader.ReadLine();
             tempRead = RemoveIPv6(tempRead);

             if (tempRead.Contains("Addresses"))
                 output += tempRead;
             else if (lastRead.Contains("Addresses"))
                 output += tempRead.Trim() + Environment.NewLine;
             else
                 output += tempRead + Environment.NewLine;
             lastRead = tempRead;
         }
         else
             garbagecan = reader.ReadLine();
         LinesRead++;
     }
 }
 return output;

The corrected regex should only allow the removal of IPv6 addresses, and leave IPv4 addresses untouched. The string that will be passed to the regex will not contain the IPv6 address(es) alone, and will almost always contain other details, and as such, it is unpredictable at which index will the addresses appear. The regex is also skipping all other IPv6 addresses after the first occuring IPv6 addresses as well for some reason, it should be noted.

Apologies if there are any missing details, I will try my best to include them in when alerted. I would also prefer working code samples, if possible, as I have almost zero knowledge regarding regex.

解决方案

(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)

Using lookarounds you can enforce a complete match rather than a partial match.See demo.

https://regex101.com/r/cT0hV4/5