简体   繁体   English

Python 2.7正则表达式:从文本字符串中提取第三个十六进制值

[英]Python 2.7 Regular Expressions: Extract 3rd Hex Value from Text String

I want to extract the 3rd hex number from the string below using regular expressions. 我想使用正则表达式从下面的字符串中提取第三个十六进制数字。 I can't seem to get the regular expressions correct. 我似乎无法使正则表达式正确。

I have a string below: 我在下面有一个字符串:

    s = ('0x11111111 0x22222222 0x33333333 0x44444444')
    x = re.compile((0x)+(\w+))

Code: 码:

    value = re.search(x, s)
    if value:
            result = int(value.group(2),16)
            print hex(result) 

You want to use re.findall 您想使用re.findall

s = '0x11111111 0x22222222 0x33333333 0x44444444'
x = re.compile(r'\w+')

# Use find all to get a list of all matching elements
values = x.findall(s)
if values:
        # Extract the wanted element (you should really check
        # len of string or better catch IndexError)
        result = int(values[2], 16)
        print(hex(result))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM