简体   繁体   中英

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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