Pythons re.compile是否需要与re.search不同的语法?

Pythons re.compile是否需要与re.search不同的语法?,python,regex,python-2.7,Python,Regex,Python 2.7,我正在玩一个程序,如果通过Paramiko发送的命令的输出中存在某个值,则需要触发该程序,在本例中是一个特定的SSID 我从命令获得的输出如下所示: [u'{\n', u" 'autoname' => 0,\n", u" 'class' => 'itfhw',\n", u" 'data' => {\n", u" 'ap_bridgemode' => 'none',\n", u"

我正在玩一个程序,如果通过Paramiko发送的命令的输出中存在某个值,则需要触发该程序,在本例中是一个特定的SSID

我从命令获得的输出如下所示:

[u'{\n', u"          'autoname' => 0,\n", u"          'class' => 'itfhw',\n", u"          'data' => {\n", u"                      'ap_bridgemode' => 'none',\n", u"                      'bridge' => '',\n", u"                      'client_isolation' => 0,\n", u"        'comment' => '',\n", u"                      'crypto_alg' => 'aes',\n", u"                      'description' => 'Remote Wireless Network',\n", u"                      'dot11r' => 0,\n", u"            'dynamic_vlan' => 0,\n", u"                      'encryption_mode' => 'wpa2_personal',\n", u"                      'freq_bands' => 'a',\n", u"                      'hardware' => 'wlan1',\n", u"                  'hide_ssid' => 0,\n", u"                      'interface_name' => 'wifi1',\n", u"                      'mac' => '00:1a:8c:0a:73:01',\n", u"                      'mac_filter' => 'disable',\n", u"              'mac_list' => '',\n", u"                      'mesh_id' => '',\n", u"  'mesh_mode' => 'none',\n", u"                      'mesh_subtag' => '',\n", u"                      'name' => 'wlan1 (Remote Wireless Network)',\n", u"                      'network_mode' => 'mixed_bgn',\n", u"                      'network_name' => 'Test1',\n", u"                      'psk' => 'secretpw',\n", u"       'r0kh_secret' => 'o2ZT4VYEYB7hhlfQnHmJQONGYnvY12',\n", u"              'ssid' => 'HACKME',\n", u"                      'ssid_vlantag' => '',\n", u"                      'status' => 1,\n", u"                  'time_scheduling' => 0,\n", u"                      'time_select' => [],\n", u"                      'utf8_ssid' => 1,\n", u"               'vlantag' => 101,\n", u"                      'wep128' => '',\n", u"   'wep_authentication' => 'open'\n", u'                    },\n', u"     'hidden' => 0,\n", u"          'lock' => '',\n", u"          'nodel'
=> '',\n", u"          'ref' => 'REF_ItfAweTest1',\n", u"          'type' => 'awe_network'\n", u'        }\n']
'ssid' => 'HACKME',\n", u"
'ssid' => 'HACKME'
这就是我需要搜索ssid名称的地方,在本例中,HACKME是为了触发程序的下一部分。这部分看起来像这样:

[u'{\n', u"          'autoname' => 0,\n", u"          'class' => 'itfhw',\n", u"          'data' => {\n", u"                      'ap_bridgemode' => 'none',\n", u"                      'bridge' => '',\n", u"                      'client_isolation' => 0,\n", u"        'comment' => '',\n", u"                      'crypto_alg' => 'aes',\n", u"                      'description' => 'Remote Wireless Network',\n", u"                      'dot11r' => 0,\n", u"            'dynamic_vlan' => 0,\n", u"                      'encryption_mode' => 'wpa2_personal',\n", u"                      'freq_bands' => 'a',\n", u"                      'hardware' => 'wlan1',\n", u"                  'hide_ssid' => 0,\n", u"                      'interface_name' => 'wifi1',\n", u"                      'mac' => '00:1a:8c:0a:73:01',\n", u"                      'mac_filter' => 'disable',\n", u"              'mac_list' => '',\n", u"                      'mesh_id' => '',\n", u"  'mesh_mode' => 'none',\n", u"                      'mesh_subtag' => '',\n", u"                      'name' => 'wlan1 (Remote Wireless Network)',\n", u"                      'network_mode' => 'mixed_bgn',\n", u"                      'network_name' => 'Test1',\n", u"                      'psk' => 'secretpw',\n", u"       'r0kh_secret' => 'o2ZT4VYEYB7hhlfQnHmJQONGYnvY12',\n", u"              'ssid' => 'HACKME',\n", u"                      'ssid_vlantag' => '',\n", u"                      'status' => 1,\n", u"                  'time_scheduling' => 0,\n", u"                      'time_select' => [],\n", u"                      'utf8_ssid' => 1,\n", u"               'vlantag' => 101,\n", u"                      'wep128' => '',\n", u"   'wep_authentication' => 'open'\n", u'                    },\n', u"     'hidden' => 0,\n", u"          'lock' => '',\n", u"          'nodel'
=> '',\n", u"          'ref' => 'REF_ItfAweTest1',\n", u"          'type' => 'awe_network'\n", u'        }\n']
'ssid' => 'HACKME',\n", u"
'ssid' => 'HACKME'
如果我使用以下代码

from re import search as re_search
ssid = 'HACKME'
#lots of guff removed
if not re_search('\'ssid\' =\> \'' + ssid +'\'', str(stdout.readlines())):
    continue
else:
    print 'SSID found - let's do something
一切正常。但是,如果我改为使用此选项:

import re
ssid = 'HACKME'
# lots of guff removed
ssidRegex = re.compile('\'ssid\' => \'' + ssid +'\'')
ssidresult = ssidRegex.search(str(stdout.readlines))
if not ssidresult:
    continue
else:
    print 'SSID found - let's do something'
我可以在同一个输出上逐个运行这两个命令,但它只会在“未编译”版本的命令上触发。现在这让我抓狂

在执行过程中,我甚至在代码中添加了一个“print ssidRegex.pattern”,我觉得这很好。这一定意味着出了严重的问题。输出如下所示:

[u'{\n', u"          'autoname' => 0,\n", u"          'class' => 'itfhw',\n", u"          'data' => {\n", u"                      'ap_bridgemode' => 'none',\n", u"                      'bridge' => '',\n", u"                      'client_isolation' => 0,\n", u"        'comment' => '',\n", u"                      'crypto_alg' => 'aes',\n", u"                      'description' => 'Remote Wireless Network',\n", u"                      'dot11r' => 0,\n", u"            'dynamic_vlan' => 0,\n", u"                      'encryption_mode' => 'wpa2_personal',\n", u"                      'freq_bands' => 'a',\n", u"                      'hardware' => 'wlan1',\n", u"                  'hide_ssid' => 0,\n", u"                      'interface_name' => 'wifi1',\n", u"                      'mac' => '00:1a:8c:0a:73:01',\n", u"                      'mac_filter' => 'disable',\n", u"              'mac_list' => '',\n", u"                      'mesh_id' => '',\n", u"  'mesh_mode' => 'none',\n", u"                      'mesh_subtag' => '',\n", u"                      'name' => 'wlan1 (Remote Wireless Network)',\n", u"                      'network_mode' => 'mixed_bgn',\n", u"                      'network_name' => 'Test1',\n", u"                      'psk' => 'secretpw',\n", u"       'r0kh_secret' => 'o2ZT4VYEYB7hhlfQnHmJQONGYnvY12',\n", u"              'ssid' => 'HACKME',\n", u"                      'ssid_vlantag' => '',\n", u"                      'status' => 1,\n", u"                  'time_scheduling' => 0,\n", u"                      'time_select' => [],\n", u"                      'utf8_ssid' => 1,\n", u"               'vlantag' => 101,\n", u"                      'wep128' => '',\n", u"   'wep_authentication' => 'open'\n", u'                    },\n', u"     'hidden' => 0,\n", u"          'lock' => '',\n", u"          'nodel'
=> '',\n", u"          'ref' => 'REF_ItfAweTest1',\n", u"          'type' => 'awe_network'\n", u'        }\n']
'ssid' => 'HACKME',\n", u"
'ssid' => 'HACKME'
我现在希望你们中的一位能够发现我(很可能是非常)明显的错误,让我走上正轨。我知道在第一个版本的代码中,在“=”和“>”之间有一个额外的“\”,但是当我将它添加到编译版本时,我会出错,并且我似乎无法正确地将其转义出来

是的,我知道编译正则表达式可能不会给我的程序增加太多速度,但我想知道我现在做错了什么。纯粹是原则问题

stdout.readlines
之后缺少
()

ssidresult = ssidRegex.search(str(stdout.readlines()))
                                                  ^^
更新

使用
read
方法可能更合适,因为
read
将返回字符串而不是字符串列表;无需调用
str

ssidresult = ssidRegex.search(stdout.read())

发布错误报告。非常感谢!“让我更疯狂!”帕德雷肯宁汉姆,谢谢你的评论。我相应地更新了答案。