1

我需要一个正则表达式来验证一个 Web 表单字段,该字段应包含一个asdot符号中的 AS 编号,如RFC 5396中所述:

阿斯多特

  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 对象和 Java EE javax.validation.constraints.Pattern与正则表达式一起使用。

4

1 回答 1

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))?$/   

假设:不允许以
数字开头。 点后有零的数字是允许的,因为我认为例如表示为. 点后的数字中不允许有前导零,例如无效。 4 字节 AS 编号的最大值是,即asdot 表示法。0.
655361.01.00009
429496729565536*65535 + 6553565535.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
于 2013-01-08T10:29:52.490 回答