正则表达式验证AS号

时间:2013-01-07 15:56:16

标签: javascript regex java-ee

我需要一个正则表达式来验证一个网页表单字段,该字段应包含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

1 个答案:

答案 0 :(得分: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号的最大值为429496729565536*65535 + 65535,即asdot表示法中的65535.65535

作为Javascript RegExp 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