开发者

Regex for bytes separated by : problem

开发者 https://www.devze.com 2023-03-03 13:40 出处:网络
I have on input hex four byte values separated by : (something likeaabbccdd:ffffffff:aaccbbff ). How to construct regex in Java which is going to give ans开发者_StackOverflowwer if input is in correct

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})*

0

精彩评论

暂无评论...
验证码 换一张
取 消