tags:

views:

44

answers:

1

Hi all,

I need to validate shipping container numbers. There is an industry standard that says only alpha-numeric and 11 characters in length is acceptable. eg: FBXU8891735

However there is also a standard industry practice where the first 4 characters can be forward-slashes eg: ////8891735

I have 2 requirements - firstly to validate the container numbers (eg. matches()) and secondly to clean the container numbers (eg. replaceAll())

System.out.println("MSCU3720090".matches("[a-zA-Z0-9]{11}"));    //true - ok
System.out.println("////3720090".matches("[a-zA-Z0-9]{11}"));    //false - fail

System.out.println("MSCU3720090".replaceAll("[^a-zA-Z0-9]*", ""));   //MSCU3720090 - ok
System.out.println("////3720090".replaceAll("[^a-zA-Z0-9]*", ""));   //3720090 - fail

I know that for matches() I can use an alternate eg:

[a-zA-Z0-9]{11}|////[a-zA-Z0-9]{7}

However this seems ugly and I'm not sure how to use it for replaceAll().

Can someone suggest a better regex to satisfy both requirements (or one for each requirement)?

Thanks.

+1  A: 
"((?:[a-zA-Z0-9]{4}|/{4})[a-zA-Z0-9]{7})"

Then just examine the contents of capture group 1 for the number.

Anon.
If you want to shorten this, \w is the same as [a-zA-Z0-9], so you could use "((?:\w{4}|/{4})\w{7})"
Jay
`\w` also matches underscores. Though you could probably use `[^\W_]`.
Anon.