Reputation: 3
I'm getting a response from a server:
B�
QeyJhbGciOiJIUzI1NiJ9.Mzc1Mjk4MjUzNjA1.PFblnWR46sByMIZX2fmot9SB8CNYDwHyDOduIf29Gn8$04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c"5ecf7534b4ccd20100d29378*�所
After I use encoding on the response:
response = r.text.encode('utf8')
I have this in my response variable:
b'B\xe2\x80\xba\x01\nQeyJhbGciOiJIUzI1NiJ9.Mzc1Mjk4MjUzNjA1.GKjZw55OvoNpH4vOw0jMSQoCYQ3mIn4b0Hq7Ye5S3wA\x12$04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c"\x185ecf7534b4ccd20100d29378*\x06\x08\xc3\x80\xc2\xa4\xc3\x93\xe2\x82\xac\x01'
I'm struggling with getting this part 04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c using different ways like
result = re.search('\x12$(.*)\x18', response)
print(result)
result = None.
What I'm trying to achieve is to get 04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c as a text.
Any ideas or recommendations?
Edit: I've removed "**" characters from the response as it was an attempt to make necessary part bold.
Upvotes: 0
Views: 471
Reputation: 647
Escape the $
character and group the characters to avoid calling split
:
re.search('(\x12\$)(.*)(\x18)', response.decode('utf-8'))[2]
Out[38]: '04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c"'
Upvotes: 0
Reputation: 142651
If you have data in bytes then you should use search bytes
But main problem is that $
has special meaning in regex - end of line - and you have to use \$
import re
data = b'B\xe2\x80\xba\x01\nQeyJhbGciOiJIUzI1NiJ9.Mzc1Mjk4MjUzNjA1.GKjZw55OvoNpH4vOw0jMSQoCYQ3mIn4b0Hq7Ye5S3wA\x12$04059f6f-sdfs-2df2-lj2k-ce310d7e3e8c"\x185ecf7534b4ccd20100d29378*\x06\x08\xc3\x80\xc2\xa4\xc3\x93\xe2\x82\xac\x01'
result = re.search(b'\$(.*)"', data)
print(result[1].decode())
Upvotes: 1