首页 > 解决方案 > 如何使用正则表达式打印出哈希键的结果

问题描述

我正在尝试使用 re.search 搜索哈希键并打印结果。

我已经尝试了许多搜索条件,但它什么也没返回。

搜索字符串:

FLASH\hashval.key        234      -A7B865C48E-

用于检索我需要的值的字符串

match = re.search(r'^FLASH\\hashval.key.+\s+(\-\w+\-)\s+', line)

代码片段

    def _test_hashval_key(self):
        '''
        Verify that the hashval.key is correct in the DRP.
        '''
        tid = 'HashVal'
        cmd = 'drpmrs dev=/dev/ro2 filelist'
        sts, out = runcmd(cmd)
        fnd = False
        for line in out.split('\n'):
            line = line.strip()
            #FLASH\hashval.key        234      -A7B865C48E-
            match = re.search(r'^FLASH\\hashval.key.+\s+(\-\w+\-)\s+', line)
            if match:
                hash = match.group(1)
                print hash()
                fnd = True
                val = -A7B865C48E-
                val1 = -EDA6384F89-
                val2 = -BF384513DC-
                if val == hash:
                    self._passed(tid, 'hashval.key sys: %s'  % (hash))
                if val1 == hash:
                    self._passed(tid, 'hashval.key prd: %s'  % (hash))
                if val2 == hash:
                    self._passed(tid, 'hashval.key crp: %s'  % (hash))
            if not match:
                self._failed(tid, 'hashval.key not found')

这就是我想展示的例子:


Test:14 HashVal: crp -A7B865C48E-

标签: pythonpython-2.7

解决方案


我能够找出正确的正则表达式

match = re.search(r'^FLASH\\hashval.key.\s+(.*)\-', line)


推荐阅读