正则表达式切字符串

时间:2018-07-05 16:39:14

标签: python regex python-3.x

我是python的新手,而对于正则表达式还不够好,

我有这段文字:

4c000215023f3d601143013582ba2e1e1603bcb9ffff02cbc5

我想像这样使用正则表达式剪切此字符串:

4c00 // the first 4 characters
0215 // the 4 second characters
023f3d601143013582ba2e1e1603bcb9 // after the 32 characters
ffff // after the 4 characters
02cb // also the 4 characters
c5 // and finally the last two characters

我这样剪断字符串,但我不喜欢这样:

        companyId = advData[10:14]
        advIndicator = advData[14:18]
        proximityUUID = advData[18:50]
        major = int(advData[50:54], 16)
        minor = int(advData[54:58], 16)
        signalPower = int(advData[-2:], 16)

3 个答案:

答案 0 :(得分:3)

这对于正则表达式不是问题。这是一个解决方案:

text = '0201041aff4c000215023f3d601143013582ba2e1e1603bcb9ffff02cbc5'

def split_at(s, index):
    return s[:index], s[index:]

res = []
for index in (10, 8, 32, 4, 4, 2):
    first, text = split_at(text, index)
    res.append(first)

print('\n'.join(res))

输出:

0201041aff
4c000215
023f3d601143013582ba2e1e1603bcb9
ffff
02cb
c5

答案 1 :(得分:1)

s="0201041aff4c000215023f3d601143013582ba2e1e1603bcb9ffff02cbc5"
print(re.findall("^(.{10})(.{8})(.{32})(.{4})(.{4})(.{2})",s))

但是对我来说,正则表达式似乎不是解决此问题的好方法……如果您只想取下n个字符,请使用字符串切片

答案 2 :(得分:-2)

如果每个组中的字符数从未改变,则可以使用以下方法:

(。{10})(。{8})(。{32})(。{4})(。{4})(。{2})