logoff
logoff

Reputation: 3445

regex to validate AS Numbers

I need a regular expression to validate a web form field that should contain an AS number in asdot notation as explained in 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".

I want to use Javascript RegExp object and Java EE javax.validation.constraints.Pattern with regex.

Upvotes: 1

Views: 1202

Answers (1)

MikeM
MikeM

Reputation: 13641

Here's a Javascript regex that should do what you require:

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

Assumptions:
Numbers beginning 0. are disallowed.
Numbers with a zero after the dot are allowed as I presume e.g. that 65536 is represented as 1.0. Leading zeros are not allowed in the number after the dot e.g. 1.00009 is invalid.
The maximum value of a 4-byte AS number is 4294967295 which is 65536*65535 + 65535, i.e. 65535.65535 in asdot notation.

As 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

As Java Pattern:

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

Upvotes: 3

Related Questions