[英]Python remove everything in bytestring before certain character
I have the following bytestring b'removethis\\x00\\x002020.10.14\\x00\\xf2\\x00^\\n\\x84>\\x01\\x00\\x10\\x01\\x14\\x00\\x00\\x00\\x8d\\xec\\xdc0\\x1bo\\xe7\\x15^\\n\\x84>\\x01\\x00\\x10\\x01\\x04\\x9b_\\x18'
我有以下字节
b'removethis\\x00\\x002020.10.14\\x00\\xf2\\x00^\\n\\x84>\\x01\\x00\\x10\\x01\\x14\\x00\\x00\\x00\\x8d\\xec\\xdc0\\x1bo\\xe7\\x15^\\n\\x84>\\x01\\x00\\x10\\x01\\x04\\x9b_\\x18'
i want everything before the first \\x00
removed so the substring removethis
gets removed basically.我想要删除第一个
\\x00
之前的所有内容,因此子字符串removethis
基本上被删除。
So the problem is that i only want everything before the first \\x00
to get removed not any future \\x00
's that may come in the string and im not sure how to do this.所以问题是,我只希望第一个
\\x00
之前的所有内容都被删除,而不是任何可能出现在字符串中的未来\\x00
,我不知道该怎么做。
You can build a regex for this.您可以为此构建一个正则表达式。
import re
txt = "b'removethis\x00\x002020.10.14\x00\xf2\x00^\n\x84>\x01\x00\x10\x01\x14\x00\x00\x00\x8d\xec\xdc0\x1bo\xe7\x15^\n\x84>\x01\x00\x10\x01\x04\x9b_\x18'"
x = re.search("\\x00.*", txt)
print(x)
Here we find the first occurance of \\x00
and then take everything to the end of the string as well using .*
.在这里,我们找到
\\x00
的第一次出现,然后使用.*
将所有内容都带到字符串的末尾。 The result is your string minus 'removethis'结果是您的字符串减去 'removethis'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.