Regex for bytes separated by : problem
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 ans开发者_StackOverflowwer if input is in correct format or not ?
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.
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.
You might try this for arbitrary sequences of 4 bytes: [0-9a-fA-F]{8}(:[0-9a-fA-F]{8})*
精彩评论