Skip to content Skip to sidebar Skip to footer

Split String After Hex Values

My string: str1 = 'Text\x11\x05\x11MoreTEXT\x02HELLO\x011' I want to get all strings between \xYY. I would like to get a list like this: list1 = ['Text', 'MoreTEXT', 'HELLO', '1'

Solution 1:

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))

Solution 2:

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.

Post a Comment for "Split String After Hex Values"