1

I'm trying to extract hex number from this string (type str) :

mystring = b'\r\n+CUSD: 2,"062506460642063706270639002006270644062E062F06450629002006230639062F0020062706440625062A063506270644",72\r\n\r\nOK\r\n'

I tried:

hexnumber= m = re.findall(r'[0-9a-fA-F]+' , mystring)
print(hexnumber)

Output:

['b', 'C', 'D', '1', '0637064406280020063A064A0631002006450648062C0648062F000A002D0020002D0020002D000A00300030003A0627064406420627062606450629000A0030003A0631062C06480639', '72']

The output i'm looking for is :

0637064406280020063A064A0631002006450648062C0648062F000A002D0020002D0020002D000A00300030003A0627064406420627062606450629000A0030003A0631062C06480639
1
  • What makes "b", "C", "2" parts invalid? Is it because they are too short? Or is it because they are not surrounded by double quotes? You need to come up with a more precise pattern that you are looking for. Commented Dec 25, 2021 at 13:14

1 Answer 1

1

You "string" is not a string with actually bytes (b'…'), so you should probably decode according to the used encoding (I assumed utf-8 here).

Then I also assumed you want to extract the string in between quotes, so I suggest using a regex with lookarounds:

import re
out = re.findall(r'(?<=")[a-fA-F\d]+(?=")', mystring.decode('utf-8'))
if out:
    print(out[0])

# 062506460642063706270639002006270644062E062F06450629002006230639062F0020062706440625062A063506270644

You can also set a minimum number of characters in your pattern (here 8 or more):

re.findall(r'[a-fA-F\d]{8,}', mystring.decode('utf-8'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.