1

I have on input hex four byte values separated by : (something like aabbccdd:ffffffff:aaccbbff ). How to construct regex in Java which is going to give answer if input is in correct format or not ?

3 Answers 3

6

How about this?

String value = "aabbccdd:ffffffff:aaccbbff";
boolean match = value.matches("\\p{XDigit}{8}(:\\p{XDigit}{8})*");
// ...

The \p{XDigit} equals to [0-9a-fA-F] by the way. See also the java.util.regex.Pattern javadoc.

Sign up to request clarification or add additional context in comments.

1 Comment

"\\p{XDigit}{8}(?::\\p{XDigit}{8})*" would be better - no unnecessary group capturing
1

You are looking for this: ^([0-9a-fA-F]{8}:)*([0-9a-fA-F]{8})$

Optional 8 HEX digits followed by a colon (:). Ending with a group without the colon. Probably a way to reduce the RegEx.

Comments

1

You might try this for arbitrary sequences of 4 bytes: [0-9a-fA-F]{8}(:[0-9a-fA-F]{8})*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.