python - 正则表达式剪切字符串
问题描述
我是 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)
解决方案
这对正则表达式来说不是问题。这是一个解决方案:
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
推荐阅读
- html - 羽毛。如何将初始页面重定向到视图文件夹而不是公用文件夹中的页面
- angular - 具有多个动作角度的效果
- responsive-design - 如何在 Tailwind 中将元素设置为显示在中屏及下方?
- ios - 我们如何在 Swift(iOS) 中的 AppleWatch 扩展中进行水平滚动
- powerbi - 任何人都知道为什么最近与 SharePoint 在线列表的连接停止了?
- android - 传感器总是在一秒钟内给我 100 个事件值
- dataframe - 从数据框的行中删除仅包含数字的字符串
- python - 没有权限。joblib 将以串行模式运行
- python - python - 如何基于单个键组合两个字典列表:python中的值?
- amazon-web-services - AWS 放大环境变量与 AWS 机密