简体   繁体   中英

re.search not matching the byte string

I have a string object got from pexpect's session.before .

From this I am trying re.search for getting the device id using the below regular expression. But it is not matching.

print (str(sess.before))
b'\r\n* daemon not running. starting it now on port 5037 *\r\n* daemon started successfully *\r\n353651\tdevice\r\n\r\n\r\n'

device_id = re.search ('([0-9a-zA-Z]+)[\t ]+device', str(sess.before))
print (device_id)
None

The str() call converts your bytes object to a representation . Tabs will be represented as the character sequence '\\' and 't' , not an actual tab:

>>> str(b'\t')
"b'\\t'"

Don't hammer your bytestring to a string like that. Either decode from bytes to a string, or just use a bytes regular expression:

device_id = re.search(b'([0-9a-zA-Z]+)[\t ]+device', sess.before)

Now device_id is the match object; you may want to call .group(1) on that:

>>> import re
>>> before = b'\r\n* daemon not running. starting it now on port 5037 *\r\n* daemon started successfully *\r\n353651\tdevice\r\n\r\n\r\n'
>>> re.search(b'([0-9a-zA-Z]+)[\t ]+device', before)
<_sre.SRE_Match object; span=(89, 102), match=b'353651\tdevice'>
>>> re.search(b'([0-9a-zA-Z]+)[\t ]+device', before).group(1)
b'353651'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM