0

I have hex strings split by the occurance of 'ff'. Unfortunately, f can also occur within a string, and, most importantly, at the end of a string (it cannot occur at the beginning). I need to split a string such that:

90500303040fff90500303040fff

is split into:

90500303040f
90500303040f

Whats a string-based way of going about this? (currently I'm doing it on byte level, but I'd like to learn a clean string way)

2
  • 1
    Are all the hex values of equal length? Commented Sep 14, 2015 at 15:10
  • If not, you could do e.g. re.split('ff(!?f)', ...) Commented Sep 14, 2015 at 15:14

1 Answer 1

1
>>> [e for e in re.split(r'(.*?f)?ff', '90500303040fff90500303040fff') if e]
['90500303040f', '90500303040f']

Or,

>>> re.sub(r'(f?)ff', r'\1\n', '90500303040ff90500303040fff').splitlines()
['90500303040', '90500303040f']

Or, non regex:

>>> '90500303040fff90500303040fff'.replace('fff', 'f\n').replace('ff','\n').splitlines()
['90500303040f', '90500303040f']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.