Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用python从字符串中提取十六进制值?_Python_Regex - Fatal编程技术网

如何用python从字符串中提取十六进制值?

如何用python从字符串中提取十六进制值?,python,regex,Python,Regex,下面是字符串 寄存器地址=[0x0128],数据=[0x59] 我想通过使用正则表达式得到0x59而不是0x0128 这是我的密码 import re data = "Register addr = [0x0128], data = [0x59]" pattern = re.compile("d[.]]") re.findall(pattern, data) 我不知道要确切的0x59,你能帮我吗?你能帮我吗 data = "Register addr = [0x0128], data = [0

下面是字符串 寄存器地址=[0x0128],数据=[0x59]

我想通过使用正则表达式得到0x59而不是0x0128

这是我的密码

import re
data = "Register addr = [0x0128], data = [0x59]"
pattern = re.compile("d[.]]")
re.findall(pattern, data)
我不知道要确切的0x59,你能帮我吗?

你能帮我吗

data = "Register addr = [0x0128], data = [0x59]"
print(re.findall(r'(?<=data\s=\s)\[(\w*)\]',data))
您还可以执行printre.searchr'?您可以执行

data = "Register addr = [0x0128], data = [0x59]"
print(re.findall(r'(?<=data\s=\s)\[(\w*)\]',data))

您也可以执行printre.searchr'?如果您只想查找0x59,只需直接搜索它

pattern = re.compile(r"0x59")
如果要查找位于[]内以0x开头的所有十六进制代码


如果您只想查找0x59,请直接搜索它

pattern = re.compile(r"0x59")
如果要查找位于[]内以0x开头的所有十六进制代码


最简单的方法是:

print(re.sub(']', '', data.partition("data = [")[2]))
.partition在data=[给出一个元组&[2]时分割字符串,选择data=[后的字符串部分

re.sub从该字符串中删除所有的“]


注意,0x59和0x0128都是有效的十六进制数。

简单的方法是通过:

print(re.sub(']', '', data.partition("data = [")[2]))
.partition在data=[给出一个元组&[2]时分割字符串,选择data=[后的字符串部分

re.sub从该字符串中删除所有的“]


请注意,0x59和0x0128都是有效的十六进制数。

0x59是您要提取的唯一字符串吗?或者它可能会更改?0x59是否始终后跟单词“data”和“=”,并且在方括号内?是否仅提取@Mox@moys是的,它始终遵循反转字符串、反转模式、提取第一个匹配项或使用finditer/findall和适当的e模式并保留最后一个。可能r'0x[^\]]+'0x59是您要提取的唯一字符串吗?或者它可能会改变吗?0x59是否总是后跟单词'data'&'='并在方括号内?仅提取@Mox@moys是的,始终遵循反转字符串、反转模式、提取第一个匹配项。或者将finditer/findall与适当的模式一起使用并保留最后一个。可能是r'0x[^\]+'