简体   繁体   English

十六进制值后拆分字符串

[英]Split string after hex values

My string: 我的字符串:

 str1 = "Text\x11\x05\x11MoreTEXT\x02HELLO\x011"

I want to get all strings between \\xYY. 我想获得\\ xYY之间的所有字符串。 I would like to get a list like this: 我想得到一个这样的列表:

list1 = ["Text", "MoreTEXT", "HELLO", "1"]

Notice that \\xYY can occure more times in a row like in my example at the beginning. 请注意,\\ xYY可以在一行中出现更多次,就像在我的示例中一样。 How can I split it like I want it? 我怎么能像我想要的那样拆分呢?

You're close! 你很亲密!

str_split = re.split('[\x01-\x1f\x7f]', str1)
str_lst = [s for s in str_split if s != '']

The first line splits the list, however it returns '' list elements if there are consecutive delimiters. 第一行拆分列表,但如果有连续的分隔符,则返回''列表元素。 The second line removes the blank elements from the list. 第二行从列表中删除空白元素。

Try this technique. 尝试这种技术。 Just substitute the hex values with spaces, then split at a series of spaces. 只需用空格替换十六进制值,然后在一系列空格中拆分。

new_string = re.split('\s+', re.sub('[\x01-\x1f\x7f]', ' ', str1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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