Thursday, 22 August 2013

Regular expression for x number of digits and only one hyphen?

Regular expression for x number of digits and only one hyphen?

I made the following regex:
(\d{5}|\d-\d{4}|\d{2}-\d{3}|\d{3}-\d{2}|\d{4}-\d)
And it seems to work. That is, it will match a 5 digit number or a 5 digit
number with only 1 hyphen in it, but the hyphen can not be the lead or the
end.
I would like a similar regex, but for a 25 digit number. If I use the same
tactic as above, the regex will be very long.
Can anyone suggest a simpler regex?
Additional Notes: I'm putting this regex into an XML file which is to be
consumed by an ASP.NET application. I don't have access to the .net
backend code. But I suspect they would do something liek this:
Match match = Regex.Match("Something goes here", "my regex",
RegexOptions.None);

No comments:

Post a Comment