我必须自动插入一个连字符在9位数字的文本更改事件在c#中,而不是javascript。
因此,如果我的号码是123456789,那么它会自动变成12345-6789。
我想使用regex.match。
我的尝试:
正则表达式"^\d{5}(-\d{4})?$“应该是结果。
所以,
Regex regTest = new Regex("^\\d{5}(-\\d{4})?$");
Match match = regTest.Match(s);
if (match.Success)
{
var numString = match.Value;
}但上述操作不会返回成功。
谢谢你的帮助。
发布于 2011-01-01 19:27:03
您的代码示例只是检查格式是否为xxxxx-xxxx。它不会插入连字符。
不需要RexEx即可插入连字符:
myString.Insert(5, "-");发布于 2011-01-01 19:28:13
正则表达式似乎是正确的。您可以在此处进行验证:
http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
最有可能的是,您没有插入'-‘然后进行匹配。
https://stackoverflow.com/questions/4573892
复制相似问题