We help IT Professionals succeed at work.

Regular expression to recognize hexadecimal 32 bytes

Johannne1
Johannne1 asked
on
Hi,
I am looking for a regular expression to recognize 32 bytes of a hexadecimal
data that ocurrs on one line:
0798888e7be5038b464c1146566702cd
The length is always the same and ther is an end of line.
I know how to recognize an exact # for digits and an exact # for characters but not
a mix.

^(\d{32}+$) - would look for exactly 32 digits followed by end of line.
the problem is that I want to look for a mix of digits and characters.
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015
Commented:
Perhaps:
^[0-9a-fA-F]{32}$

Open in new window

Most Valuable Expert 2011
Top Expert 2015

Commented:
>>  ^(\d{32}+$)

Actually, because you added the plus, that pattern would look for one-or-more sets of 32 digits (e.g. 32, 64, 96, etc.)

Author

Commented:
Hi Kaufmed,
Thanks for  the email, I find it interesting that you say that + add one more set.

For example, ^(((\d{14}+)((;){1}))\d$)
if I want to look for exactlly 14 digits followed by exactly 1 semicolon followed by digits ended by a
number sign. then I should take out the + have the {14} right?

I am testing your suggestion 1:
 1: ^[0-9a-fA-F]{32}$
 
Do I have to include 1: ? I assume not 'cause the regular expression starts with the carret?
please try

import java.util.*;
import java.util.regex.*;


public class HexaCheck
{
    public static void main(String args[])
    {
            String patternStr = "[0-9A-Z]{32}";
            String inputString = "123456789012345678901234567890AB";
            Pattern pattern = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(inputString);
            System.out.println("Pattern Matching : " + matcher.matches());
    }


}

Open in new window

Most Valuable Expert 2011
Top Expert 2015

Commented:
I only took a quick glance at the syntax originally, but now looking at it again, "{32}+" is actually invalid syntax and should generate an error when you try to build your regex object. Both "{32}" and "+" are quantifiers and cannot be used back-to-back. For "one-or-more sets of 32 digits," you would have to modify the patter to use grouping:

    ^(\d{32})+$

Notice the parentheses surround the digit pattern with its quantifier, then the plus modifies everything inside the parentheses (since it immediately follows the closing paren).

For "if I want to look for exactlly 14 digits followed by exactly 1 semicolon followed by digits ended by a
number sign," I am not exactly sure what you mean by "number sign," but I am going to take that as "#", and so the pattern would be:

    \d{14};\d+\#

Notice that it is a bit redundant to do ";{1}", so I left "{1}" off  :)