0

I wanted to make a validator for amazon following these specification http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-keys.

And I created my pattern as:

private static Pattern objectKeyNamePtrn = Pattern.compile("^[a-z0-9_-!.*'()]{6,30}$");  

But, I am getting error:

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.util.regex.PatternSyntaxException: Illegal character range near index 11 ^[a-z0-9_-!.*'()]{6,30}$

I tried escaping with '\' but didn't work.

And also Amazon guys says, The name for a key is a sequence of Unicode characters whose UTF-8 encoding is at most 1024 bytes long.

How should I check that, using regular expression?

Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36

1 Answers1

1

I think the issue is here: "_-!". The compiler is trying to interpret that sequence as a range of characters instead of individual characters.

Try reordering them, or use an escape character like so: "_\-!".

Two backslashes are required to create the escape character in java.

Edit: to check if the string is less than 1024 bytes, see this question: bytes of a string in java?

Community
  • 1
  • 1
dukr
  • 26
  • 2