Home > Software engineering >  Regex to find hex value greater than a specific value
Regex to find hex value greater than a specific value

Time:08-27

Hi I am trying to find a regex that would match occurances greater than a particular value. I am currently using this to match instances greater than 0. I want to get the match only if its greater than 5.which means 0x2, 0x1 and 0x4 wouldnt be matched below

Error.*[1-9|A-F]

Errors : 0x22
Errors : 0x2
Errors : 0x67
Errors : 0xA1
Errors : 0x5
Errors : 0x4
Errors : 0x0 Not matched 
Errors : 0x1

Thanks.

CodePudding user response:

For the examples, if 0x2, 0x1 and 0x4 should not be matched but starting at 0x5 you might use:

\bErrors\s*:\s*0x(?:[5-9A-F]|[A-F0-9]{2,})\b

Explanation

  • \bErrors\s*:\s*
  • 0x Match literally
  • (?: Non capture group for the alternation
    • [5-9A-F] Match either a digit 5-9 or char A-F
    • | Or
    • [A-F0-9]{2,} Match 2 or more occurrences of a char A-F or a digit 0-9
  • ) Close the non capture group
  • \b A word boundary

Regex demo

  • Related