2

I am trying to detect if a string is a Hex Code (please let me know if there is any better way to do it), but to begin with I started to check if the string I'm looking at is a number 0-9 of length 2:

def is_hex(text):
    if re.match('^[0-9]{0,2}',text):
        print("The text \n" + text + "\n has been identified as HEX code")
        print(re.search('^[0-9]{0,2}',text))
    else:
        print("The text \n" + text + "\n has >NOT< been identified as HEX code")

Then I run some tests, but ANY string will go through, just like this one "Proba de Merda":

The text Proba de Merda has been identified as HEX code Evaluate next item

Am I missing something?

Thanks

Andrew McDowell
  • 2,860
  • 1
  • 17
  • 31
Alejandro A
  • 1,150
  • 1
  • 9
  • 28
  • 2
    `'^[0-9]{0,2}'` matches an empty string. You need `'^[0-9]{1,2}'` to match 1 or 2 digits at the start or just `[0-9]+` (with `re.match`, it will require one or more digits at the start of the string) – Wiktor Stribiżew Feb 28 '19 at 10:15
  • @alejandro-a check this topic >> https://stackoverflow.com/questions/11592261/check-if-a-string-is-hexadecimal it gives 2 alternatives for checking if the string is a hex string. in your code you forget to check the alphabetic characters [a-f] – Hichem BOUSSETTA Feb 28 '19 at 10:18

0 Answers0