在C#中使用Regex进行ip:端口格式化 [英] Using Regex in C# for ip:port format
问题描述
大家好,我正在使用C#.Net 2008.
我正在使用以下代码来修改正确的格式(udp ip:port即225.1.1.1:3000)输入字符串。
但它无法正常工作。
System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@\d {1,3} 。\d {1,3}。\\\ {1,3}。\\\ {1,3}:\d {1,5});
bool b;
b = regStr.IsMatch(225.1.1.1:3000); //返回true
b = regStr.IsMatch(2225.1.1.1:3000); //返回true。应该返回false
任何人都知道正确的正则表达式为ip:port格式?
任何人都可以给我很好的链接,使用正则表达式在C#中使用正则表达式。
提前感谢。问题,
Aniket A. Salunkhe
这种行为是正确的,因为您编写的正则表达式检查输入字符串中包含的匹配项。 br>如果只想匹配整个字符串,则必须在开头添加^,并在正则表达式结尾处添加
。
例如:^ \\ \\d {1,3} .\d {1,3} .\d {1,3} .\d {1,3}:{\d 1,5}
Hi all,
I am using C#.Net 2008.
I am using following code to varify proper format (udp ip:port i.e "225.1.1.1:3000") of input string.
But it is not working properly.
System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}");
bool b;
b = regStr.IsMatch("225.1.1.1:3000"); //return true
b = regStr.IsMatch("2225.1.1.1:3000"); //return true. should return false
Anybody knows proper regular expression for ip:port format?
Can anyone give me good links for using Regex , regular expression in C#.
Thanks in advance.
Regards,
Aniket A. Salunkhe
This behavior is correct, because the regexp you wrote checks for matches contained in the input string.
If you want to match the whole string only, you must add ^ at the begin and
at the end of your regex.
For Example: "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}
"
这篇关于在C#中使用Regex进行ip:端口格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!