我需要一个正则表达式来验证 Web 表单字段,该字段应包含 asdot
表示法中的 AS 编号,如 RFC 5396 中所述。 :
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用正则表达式。
最佳答案
这是一个 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
,即asdot表示法的65535.65535
。
作为 Javascript RegExp 对象:
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
关于javascript - 用于验证 AS 编号的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199418/