为什么在这种情况下,re.findall()返回一个空列表,而另一个返回一个空字符串的列表?

时间:2019-02-20 02:28:02

标签: python regex

{ "schema": { "type": "struct", "fields": [ { "type": "struct", "fields": [ { "type": "string", "optional": false, "field": "Id" }, { "type": "string", "optional": false, "field": "UserId" }, { "type": "string", "optional": false, "field": "RoleId" }, { "type": "string", "optional": true, "field": "APPID" }, { "type": "int32", "optional": false, "default": 0, "field": "IsDeleted" }, { "type": "int64", "optional": false, "name": "io.debezium.time.Timestamp", "version": 1, "default": 0, "field": "ctime" }, { "type": "int64", "optional": false, "name": "io.debezium.time.Timestamp", "version": 1, "default": 0, "field": "utime" } ], "optional": true, "name": "local3910472223.br_auths.auths_user_roles.Value", "field": "before" }, { "type": "struct", "fields": [ { "type": "string", "optional": false, "field": "Id" }, { "type": "string", "optional": false, "field": "UserId" }, { "type": "string", "optional": false, "field": "RoleId" }, { "type": "string", "optional": true, "field": "APPID" }, { "type": "int32", "optional": false, "default": 0, "field": "IsDeleted" }, { "type": "int64", "optional": false, "name": "io.debezium.time.Timestamp", "version": 1, "default": 0, "field": "ctime" }, { "type": "int64", "optional": false, "name": "io.debezium.time.Timestamp", "version": 1, "default": 0, "field": "utime" } ], "optional": true, "name": "local3910472223.br_auths.auths_user_roles.Value", "field": "after" }, { "type": "struct", "fields": [ { "type": "string", "optional": true, "field": "version" }, { "type": "string", "optional": false, "field": "name" }, { "type": "int64", "optional": false, "field": "server_id" }, { "type": "int64", "optional": false, "field": "ts_sec" }, { "type": "string", "optional": true, "field": "gtid" }, { "type": "string", "optional": false, "field": "file" }, { "type": "int64", "optional": false, "field": "pos" }, { "type": "int32", "optional": false, "field": "row" }, { "type": "boolean", "optional": true, "default": false, "field": "snapshot" }, { "type": "int64", "optional": true, "field": "thread" }, { "type": "string", "optional": true, "field": "db" }, { "type": "string", "optional": true, "field": "table" }, { "type": "string", "optional": true, "field": "query" } ], "optional": false, "name": "io.debezium.connector.mysql.Source", "field": "source" }, { "type": "string", "optional": false, "field": "op" }, { "type": "int64", "optional": true, "field": "ts_ms" } ], "optional": false, "name": "local3910472223.br_auths.auths_user_roles.Envelope" }, "payload": { "before": null, "after": { "Id": "DB4DA841364860D112C3C76BDCB36635", "UserId": "0000000000", "RoleId": "5b7e5f9b4bc00d89c4cf96ae", "APPID": "br.region2", "IsDeleted": 0, "ctime": 1550138524000, "utime": 1550138524000 }, "source": { "version": "0.8.3.Final", "name": "local3910472223", "server_id": 0, "ts_sec": 0, "gtid": null, "file": "mysql-bin.000003", "pos": 64606, "row": 0, "snapshot": true, "thread": null, "db": "br_auths", "table": "auths_user_roles", "query": null }, "op": "c", "ts_ms": 1550568556614 } } 返回一个空列表re.findall(r'[^-](-*)$', '----')

同时,[]返回带有空字符串re.findall(r'[^-](-*)$', '----343')的列表。

在两种情况下均不匹配。为什么re.findall()在第一种情况下返回一个空列表,而在第二种情况下返回一个带有空字符串的列表?

2 个答案:

答案 0 :(得分:1)

$与该行的末尾匹配,该末尾有零个字符。在第一种情况下,没有匹配项,因为该行的末尾是破折号(-),而您已将其与[^-]取反。在第二行中,该行的末尾是与正则表达式匹配的非破折号字符。

答案 1 :(得分:1)

之所以这样做,是因为当您说public function aggiungiDocumento(){ $nomeFile = $this->input->post('nomeFile'); echo $nomeFile; $document = $_FILES['document']['name']; echo $document; if($document=''){ } else { echo getcwd() . "\n"; $config['upload_path']='./aziende'; echo $config['upload_path']; $config['allowed_types']='jpg|gif|png'; //new file name process $tmp = explode('.', $document); $ext = end($tmp); $config['file_name'] = $nomeFile.$ext; $this->load->library('upload', $config); if(!$this->upload->do_upload('document')){ echo "nope"; } else{ echo "yup"; $config['file_name'] = $nomeFile; echo $config['file_name']; $document=$this->upload->data('file_name'); } } 时,您匹配的是或出现的(-*),只要这些连字符后面跟随的字符不是{{1} }。在第一个示例中,没有不属于-的字符,因此没有匹配项,因此为空列表。在第二个示例中,有三个不是连字符的字符(数字),因此-与最后一个-匹配,后跟(-*)的零出现。