-1

In rubular iam using regex 'docsBpi2CmDeviceManufCert.2 = Hex-STRING:(\s\w.*$\s\w.*)+' to find hex values from below string and getting the expected value

String

docsBpi2CmDeviceManufCert.2 = Hex-STRING: 30 82 03 D1 30 82 02 B9 A0 03 02 01 02 02 10 60 
F5 8D 9C E7 FF BC D8 79 AF 4E D7 B3 76 1E 7F 30 
0D 06 09 2A 86 48 86 F7 0D 01 01 05 05 00 30 81 
EC DC 34 84 EE 

enter image description here But following sample java code not returns the pattern

Pattern patternToMatch = Pattern.compile("docsBpi2CmDeviceManufCert.2 = Hex-STRING:(\\s\\w.*$\\s\\w.*)+");
        String response11 = "docsBpi2CmDeviceManufCert.2 = Hex-STRING: 30 82 03 D1 30 82 02 B9 A0 03 02 01 02 02 10 EC DC 34 84 EE";
        String matchedString = "";
        // instance of pattern for match
        Pattern pattern = null;
        // Instance of matcher
        Matcher matcher = null;
        if (null != response11) {
            matcher = patternToMatch.matcher(response11.trim());
            if (matcher.find()) {
                matchedString = matcher.group(1);
            }
        }

Whats wrong with my code

Psl
  • 3,830
  • 16
  • 46
  • 84
  • 3
    Why do you have `$` in the middle of the capture group? Your regex works only when the string contains `\n` – Amongalen Jul 20 '20 at 07:19
  • Can u answer to my question rather than comment.So that I can accept ur answer..When i removed $ sign it worked..thanks. – Psl Jul 20 '20 at 07:42

1 Answers1

-1

In the capture group you have a $ character. It tries to match an end of the line. It is followed by some other expressions to match. Because of that, your regex will only work for multiline input string. If you only want to match a singleline string, you should remove the $ for it to work.

Amongalen
  • 3,101
  • 14
  • 20