問題:.NET正則表達式字符串如何提取IPv6地址?.NET正則表達式:如何提取IPv6地址部分
我可以得到它提取一個簡單的IPv6地址,如「1050:0:0:0:5:600:300c:326b」,但不是冒號格式(「ff06 :: c3」);
我的問題是,它應該提取一個0之間的每個省略值:: 我該怎麼做?
下面我的代碼+說明。
通過省略前導零指定IPv6地址。
例如,IPv6地址1050:0000:0000:0000:0005:0600:300c:326b
可以寫爲1050:0:0:0:5:600:300c:326b。
雙冒號
使用雙冒號(:)代替一系列零指定IPv6地址。
例如,IPv6地址ff06:0:0:0:0:0:0:c3
可寫爲ff06 :: c3。
雙冒號只能在IP地址中使用一次。
strInputString = "ff06::c3";
strInputString = "1050:0000:0000:0000:0005:0600:300c:326b";
string strPattern = "([A-Fa-f0-9]{1,4}:){7}([A-Fa-f0-9]{1,4})";
//strPattern = @"\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\z";
//strPattern = @"(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\Z)|(\A(([0-9a-f]{1,4}:){1,7}|:):\Z)|(\A:(:[0-9a-f]{1,4}){1,7}\Z)|(\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|(\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|(\A([0-9a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d?\d)){3}\z";
//strPattern = @"/^(?:(?:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9](?::|$)){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))$/i";
System.Text.RegularExpressions.Regex reValidationRule = new System.Text.RegularExpressions.Regex("^" + strPattern + "$");
if (reValidationRule.Match(strInputString).Success) // If matching pattern
{
System.Text.RegularExpressions.Match maResult = System.Text.RegularExpressions.Regex.Match(strInputString, strPattern);
// Console.WriteLine(maResult.Groups.Count)
string[] astrReturnValues = new string[4];
System.Text.RegularExpressions.GroupCollection gc = maResult.Groups;
System.Text.RegularExpressions.CaptureCollection cc;
int counter;
//System.Web.Script.Serialization.JavaScriptSerializer jssJSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//Console.WriteLine(jssJSONserializer.Serialize());
// Loop through each group.
for (int i = 0; i < gc.Count; i++)
{
Console.WriteLine("Group: {0}", i);
cc = gc[i].Captures;
counter = cc.Count;
// Print number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString());
// Loop through each capture in group.
for (int ii = 0; ii < counter; ii++)
{
Console.WriteLine("Capture: {0}", ii);
// Print capture and position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
這個問題對你有幫助嗎? http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses – Greg 2010-05-28 20:03:42
不,這是驗證。我需要提取IP,以便我有: Number0 = 1050; Number1 = 0000; Number2 = 0000; Number3 = 0000; Number4 = 0005; Number5 = 0600; Number6 = 300c; Number7 = 326b; – 2010-05-29 10:23:03
是否需要使用Regex?爲什麼不使用** byte [] ipBytes = IPAddress.Parse(strInputString).GetAddressBytes()**,然後將字節以您的首選格式放在一起? – 2012-11-06 17:36:25