2013-01-07 44 views
1

我需要一個正則表達式來驗證應包含在asdot符號的AS號作爲RFC 5396說明的web表單字段:正則表達式來驗證AS編號

asdot

refers to a syntax scheme of representing AS number values less 
    than 65536 using asplain notation and representing AS number 
    values equal to or greater than 65536 using asdot+ notation. 
    Using asdot notation, an AS number of value 65526 would be 
    represented as the string "65526" and an AS number of value 65546 
    would be represented as the string "1.10". 

我想用正則表達式來使用Javascript RegExp object和Java EE javax.validation.constraints.Pattern

+0

這難以從驗證的正數與可選的小數部分是不同的。你有什麼試過,它是如何不起作用的? – tripleee

+0

@tripleee我試過一些東西,但我不是一個正則表達式專家,我發現測試簡單的數字範圍非常困難(http://www.regular-expressions.info/numericranges.html) – logoff

回答

3

這裏是一個Javascript正則表達式應該做你需要什麼:

/^([1-5]\d{4}|[1-9]\d{0,3}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(\.([1-5]\d{4}|[1-9]\d{0,3}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]|0))?$/ 

假設:
開頭的號碼0.是不允許的。
允許在點之後有零的數字,因爲我假設例如65536表示爲1.0。 點後面的數字不允許出現前導零,例如: 1.00009無效。
4字節AS編號的最大值是4294967295,即65536*65535 + 65535,即65535.65535採用asdot表示法。

如JavaScript正則表達式oject:

var asdot = new RegExp("^([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])(\\.([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]|0))?$"); 

console.log(asdot.test('65535.65535')) // true 

從Java圖形:

Pattern asdot = Pattern.compile("^([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])(\\.([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]|0))?$"); 

System.out.println(asdot.matcher("65535.65535").matches()); // true 
+0

已經過測試。只是史詩!非常感謝你,這正是我想要的。您的假設都基於RFC 5396。 – logoff