簡體   English   中英

Python 2.7正則表達式:從文本字符串中提取第三個十六進制值

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

我想使用正則表達式從下面的字符串中提取第三個十六進制數字。 我似乎無法使正則表達式正確。

我在下面有一個字符串:

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

碼:

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

您想使用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