Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Python 2.7 - Fatal编程技术网

Python正则表达式匹配字符串和打印

Python正则表达式匹配字符串和打印,python,regex,python-2.7,Python,Regex,Python 2.7,获取了以下字符串: hash=49836EC32432A9B830BECFD66A9B6F936327EAE8 我需要匹配49836EC32432A9B830BECFD66A9B6F936327EAE8,因此我需要: match = re.findall(".*hash=([A-F0-9]+).*",mydata) 很酷,但是当我想打印它的时候 print "Hash: %s" % match 我得到散列:['c5e8c500ba925237e399c44efd05bc4aaf76292'

获取了以下字符串:

hash=49836EC32432A9B830BECFD66A9B6F936327EAE8
我需要匹配
49836EC32432A9B830BECFD66A9B6F936327EAE8
,因此我需要:

match = re.findall(".*hash=([A-F0-9]+).*",mydata)
很酷,但是当我想打印它的时候

print "Hash: %s" % match
我得到
散列:['c5e8c500ba925237e399c44efd05bc4aaf76292']


我做错了什么?我需要打印
Hash:c5e8c500ba925237e399c44efd05bc4aaf76292
findall
提供字符串中所有匹配项的列表。您看到的正是一个列表,其中包含找到的匹配项

改为尝试
search
:它返回一个匹配组,您可以获得第一组:


或者您可以执行
findall
并使用列表中的第一个条目进行打印(例如
match[0]
)。

findall
提供字符串中所有匹配项的列表。您看到的正是一个列表,其中包含找到的匹配项

改为尝试
search
:它返回一个匹配组,您可以获得第一组:


或者您可以执行
findall
,并使用列表中的第一个条目进行打印(例如
match[0]
)。

它似乎没有将match的值作为字符串进行计数。我没有“mydata”,但当我将哈希保存为字符串时,它可以很好地打印出来。看起来你看到的东西不是字符串,我认为这是匹配的值。我相信引号表明这是一个字符串。执行type()以查看强制转换为什么,或者在print语句后尝试键入str(match)而不是match

编辑:


其他人注意到它正在返回一个数组值。Do match[0]返回数组的第一个值。

它似乎没有将匹配值作为字符串计算。我没有“mydata”,但当我将哈希保存为字符串时,它可以很好地打印出来。看起来你看到的东西不是字符串,我认为这是匹配的值。我相信引号表明这是一个字符串。执行type()以查看强制转换为什么,或者在print语句后尝试键入str(match)而不是match

if match:
    print "Hash: %s" % match[0]
编辑:


其他人注意到它正在返回一个数组值。Do match[0]返回数组的第一个值。

在代码中,match是由re.findall函数([1]:)生成的字符串列表。在此列表中,所有匹配项都按找到的顺序返回。在您的示例中,列表只有一个元素,即match[0]。

在代码中,match是由re.findall函数([1]:)生成的字符串列表。在此列表中,所有匹配项都按找到的顺序返回。在您的情况下,列表只有一个元素,即匹配[0]。

很简单:

if match:
    print "Hash: %s" % match[0]
In[1]: import re

In[2]: mydata = 'hash=49836EC32432A9B830BECFD66A9B6F936327EAE8'

In[3]: re.findall(".*hash=([A-F0-9]+).*",mydata)
Out[3]: ['49836EC32432A9B830BECFD66A9B6F936327EAE8'] # a list

In[4]: re.match(".*hash=([A-F0-9]+).*",mydata)
Out[4]: <_sre.SRE_Match at 0x5d79020>

In[5]: re.match(".*hash=([A-F0-9]+).*",mydata).groups()
Out[5]: ('49836EC32432A9B830BECFD66A9B6F936327EAE8',) # a tuple

In[6]: match = Out[3]

In[7]: print "Hash:",match[0] # so print the first item!!!
Hash: 49836EC32432A9B830BECFD66A9B6F936327EAE8
很简单:

In[1]: import re

In[2]: mydata = 'hash=49836EC32432A9B830BECFD66A9B6F936327EAE8'

In[3]: re.findall(".*hash=([A-F0-9]+).*",mydata)
Out[3]: ['49836EC32432A9B830BECFD66A9B6F936327EAE8'] # a list

In[4]: re.match(".*hash=([A-F0-9]+).*",mydata)
Out[4]: <_sre.SRE_Match at 0x5d79020>

In[5]: re.match(".*hash=([A-F0-9]+).*",mydata).groups()
Out[5]: ('49836EC32432A9B830BECFD66A9B6F936327EAE8',) # a tuple

In[6]: match = Out[3]

In[7]: print "Hash:",match[0] # so print the first item!!!
Hash: 49836EC32432A9B830BECFD66A9B6F936327EAE8