c# - Match pattern of [0-9]-[0-9]-[0-9], but without matching [0-9]-[0-9] -


I'm not sure how to accomplish this with a regular expression (or if I can), I New to regex). I have an angle value that the user will type and I am trying to validate the entry. This form is in degree-minute-second. The problem is that if the user typed incorrectly in the second part, Will catch up, but my match success for the degree is.

Perhaps the method will explain better:

  Private boolean isTextValid (string _angleValue) {Regex _degreeMatchPattern = New Regex ("0 * [1-9]"); Reggae's DegreeMatch Pattern = New Reggae ("(0 * [0-9] - {1} 0 * [0- 9]) {1}"); Reggae degree from these camcaptern = new reggae ("0 * [0-9] - {1} 0 * [0- 9] - {1} 0 * [0- 9]"); Match _ Dimri Match, _ Degree Meanmatch, _ Degree Minnesack Match; _degreeMinSecMatch = Degrees MinSecMatchPattern.Match (_angleValue); If (_degreeMinSecMatch.Success) return is true; _degreeMinMatch = Degree MinMatchPattern.Match (_angleValue); If (_degreeMinMatch.Success) return is true; _degreeMatch = _degreeMatchPattern.Match (_angleValue); If (_degreeMatch.Success) return is true; return false; }}  

If the degree-minute-second match is unsuccessful, I want to check the degree-minute, but if the user does not enter any second data, will I Can I do through regex, or do I need to parse the string and evaluate each part individually? Thank you.

Edit: Sample data as correct data will be 45-23-10. Problem 45-23 is also valid data; 0 seconds if the user types 45-23-1 = on the accident, the degree MinMatchPattern regex in my code will match successfully, even if it is invalid. Second edit: Just to make it clear, minutes and seconds are both optional, user can type 45 and it is valid.

First of all, a character matches a letter once by default, so {1} Is unnecessary.

Second, because you can apparently separate this value (you point to this value only, look in the paragraphs of the data entered instead) You should include ^ and $ In your string, to apply that the string should only contain this pattern.

"^ \ d {1,3} - \ d {1,2} (- \ d {1, 2})" $ "

By breaking it: ^ matches the beginning of the string \ D any matches the decimal letter, and after that you are specifying {1,3}, which is equal to any number Match three sets of three frequencies. Then you are looking for a dash, then a similar decimal pattern but only once or twice. The last word is enclosed in brackets so that we can group the letters. Its form is similar to the first two, then is one? Which marks the predecessor character group as an alternative, in the end, $ indicates that input should end, it will match 222-33-44 or 222-33, but 222-3344 or 222-33 -ABC will not be.

Keep in mind that there are additional rules that you want to include. For example, seconds can be expressed as a decimal (if you want less than a second resolution). You will need to alternatively expect the decimal point and one or more additional digits. In addition, you probably have a maximum degree value; The above integer value of regex 359-59-59 above will match the DMS value, although it will match 999-99- 99 which is not valid. You can limit the maximum value using regex (for example "(3 [0-5] \ d | [1-2] \ d {2} | \ d {1,2})" 0 to 35 9, 3, then 0-5, then 0-9, or any 3-digit number starting with 1 or 2, or any two-digit number), but as the example shows Regex will be long and dirty, so do it as well as you document it in the document.


Comments