Split a string and keep the delimiters as part of the split string chunks, not as separate list elements

后端 未结 1 425
感情败类
感情败类 2021-01-21 12:01

This is a spin-off from In Python, how do I split a string and keep the separators?

rawByteString = b\'\\\\!\\x00\\x00\\x00\\x00\\x00\\x00\\\\!\\x00\\x00\\x00\\x00\

相关标签:
1条回答
  • 2021-01-21 12:17

    You may use

    re.split(rb'(?!\A)(?=\\!)', rawByteString)
    re.split(rb'(?!^)(?=\\!)', rawByteString)
    

    See a sample regex demo (the string input changed since null bytes cannot be part of a string).

    Regex details

    • (?!^) / (?!\A) / (?<!^) - a position other than start of string
    • (?=\\!) - a position not immediately followed with a backslash + !

    NOTES

    • Since you use a byte string, the b prefix is required when defining the pattern string literal
    • r makes the string literal a raw string literal so that we do not have to double escape backslashes and can use \\ to match a single \ in the string.

    See Python demo:

    import re
    rawByteString = b'\\!\x00\x00\x00\x00\x00\x00\\!\x00\x00\x00\x00\x00\x00'
    print ( re.split(rb'(?!\A)(?=\\!)', rawByteString) )
    

    Output:

    [b'\\!\x00\x00\x00\x00\x00\x00', b'\\!\x00\x00\x00\x00\x00\x00']
    
    0 讨论(0)
提交回复
热议问题