我不知道如何用常規表達式(或者如果我可以;我是新來的正則表達式)。我有一個角度值的用戶將鍵入,我試圖驗證條目。它的形式是度 - 分 - 秒。我遇到的問題是,如果用戶錯誤地輸入了秒數,我必須找出那個錯誤,但是我的比賽分鐘是成功的。匹配模式[0-9] - [0-9] - [0-9],但沒有匹配[0-9] - [0-9]
或許該方法將更好地解釋:
private Boolean isTextValid(String _angleValue) {
Regex _degreeMatchPattern = new Regex("0*[1-9]");
Regex degreeMinMatchPattern = new Regex("(0*[0-9]-{1}0*[0-9]){1}");
Regex degreeMinSecMatchPattern = new Regex("0*[0-9]-{1}0*[0-9]-{1}0*[0-9]");
Match _degreeMatch, _degreeMinMatch, _degreeMinSecMatch;
_degreeMinSecMatch = degreeMinSecMatchPattern.Match(_angleValue);
if (_degreeMinSecMatch.Success)
return true;
_degreeMinMatch = degreeMinMatchPattern.Match(_angleValue);
if (_degreeMinMatch.Success)
return true;
_degreeMatch = _degreeMatchPattern.Match(_angleValue);
if (_degreeMatch.Success)
return true;
return false;
}
}
我要檢查度分鐘,如果度 - 分 - 秒匹配不成功,但前提是用戶沒有輸入任何秒內的數據。我可以通過正則表達式來做到這一點,還是我需要解析字符串並分別評估每個部分?謝謝。
編輯:示例數據將是45-23-10作爲正確的數據。問題是45-23也是有效的數據;理解0秒。因此,如果用戶輸入45-23-1 =意外,我的代碼中的degreeMinMatchPattern正則表達式將成功匹配,即使它是無效的。
第二次編輯:爲了清楚起見,分鐘和第二部分都是可選的。用戶可以輸入45,這是有效的。
你有一些示例數據? – 2010-09-27 17:18:18
它是45-23-1 =。我想你只是沒有看到等號。或許一個更好的例子是45-23-1A。對困惑感到抱歉。 – Shawn 2010-09-27 17:45:13