I have to automatically insert a hyphen in 9 digit number on text change event in c# only not javascript.
So if my number is 123456789 then it automatically becomes 12345-6789.
I would like to use regex.match.
My try:
The regex "^\\d{5}(-\\d{4})?$" is how the result should be.
so,
Regex regTest = new Regex("^\\d{5}(-\\d{4})?$");
Match match = regTest.Match(s);
if (match.Success)
{
var numString = match.Value;
}
But the above does not returns a success.
Thanks for help.
Your code sample simply checks that the format is xxxxx-xxxx
. It doesn't insert the hyphen.
You do not need a RexEx to insert a hyphen:
myString.Insert(5, "-");
The regular expression seems correct. You can verify it here:
http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
Most probably you are not inserting the '-' and then matching.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.