Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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
mysqldump插入的Python正则表达式_Python_Mysql_Regex - Fatal编程技术网

mysqldump插入的Python正则表达式

mysqldump插入的Python正则表达式,python,mysql,regex,Python,Mysql,Regex,我有一大堆字符串格式的查询,如下所示: 在用户值中插入1,'pb',NULL,'User 例如“,”example@example,,'da',1493878226,NULL,NULL,'das','unassigned',,,,,,,,,1,NULL,NULL,NULL,,,,,,1,0,025495,NULL,,,0,0,0,NULL,2,'pb',NULL,'User 例如“,”example@example'a774f',1493878226,NULL,NULL,'device','un

我有一大堆字符串格式的查询,如下所示:

在用户值中插入1,'pb',NULL,'User 例如“,”example@example,,'da',1493878226,NULL,NULL,'das','unassigned',,,,,,,,,1,NULL,NULL,NULL,,,,,,1,0,025495,NULL,,,0,0,0,NULL,2,'pb',NULL,'User 例如“,”example@example'a774f',1493878226,NULL,NULL,'device','unassigned',,,,,,,,,,1,NULL,NULL,NULL,,,,,,0,0,0,NULL,NULL,0,0,0,NULL,3,'p=',NULL,'User 例如“,”example@example'95fa',1493878226,NULL,NULL,'device','b',,,,,,,,1,NULL,NULL,NULL,,,,,0,0,0,NULL,NULL,,,0,0,0,NULL,4,'pa',NULL,'User 例如“,”example@example'ea1',14938782261510178200,NULL,'a','unassigned',,,,,,,,,,1,NULL,NULL,NULL,,,,,,0,0,0,NULL,NULL,0,1,0,NULL,5,'pb',NULL,'AAA','example@example'das',1493878226,NULL,NULL,'a','unassigned','dasmin.png',,,,,,1,NULL,NULL,NULL,,,,,,,0,0,0,NULL,NULL,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,

我想做的是,能够将分离的文本的每一部分分离开来,这样我就可以对它们进行迭代,更改一些内容,然后进行手动插入

我该怎么办


顺便说一句,我不擅长正则表达式…

我在Python和SQL方面做了一些工作,所以我想我知道您想做什么。试试这个:

import re

sql_text = "INSERT INTO users VALUES (1,'pb',NULL,'User Example','example@example','','da',1493878226,NULL,NULL,'das','unassigned','','','','','','','',1,'',NULL,'',NULL,'','','','',1,0,0,25495,NULL,'','',0,0,0,'',NULL,''),(2,'pb',NULL,'User Example','example@example','','a774f',1493878226,NULL,NULL,'device','unassigned','','','','','','','',1,'',NULL,'',NULL,'','','','',0,0,0,NULL,NULL,'','',0,0,0,'',NULL,''),(3,'p=',NULL,'User Example','example@example','','95fa',1493878226,NULL,NULL,'device','b','','','','','','','',1,'',NULL,'',NULL,'','','','',0,0,0,NULL,NULL,'','',0,0,0,'',NULL,''),(4,'pa',NULL,'User Example','example@example','','ea1',1493878226,1510178200,NULL,'a','unassigned','','','','','','','',1,'',NULL,'',NULL,'','','','',0,0,0,NULL,NULL,'','',0,1,0,'',NULL,''),(5,'pb',NULL,'AAA','example@example','','das',1493878226,NULL,NULL,'a','unassigned','','','dasmin.png','','','','',1,'',NULL,'',NULL,'','','','',0,0,0,NULL,NULL,'','',0,0,0,'',NULL,'');"
sql_list = re.findall("\([^)]+\)", sql_text)
for sql_item in sql_list:
    print(sql_item)
我将SQL文本和正则表达式中的每一组值放入一个列表中。正则表达式本身就是键,它是一个负字符类。它匹配每个开始的paran,包括下一个结束的paran。[^]+指所有非交割方

很明显,您不会打印sql_项,而是希望更改文本,但这应该足以让您继续

下面是一个使用正则表达式的好站点:

如果我正确理解您需要单独设置每个插入值,您可以尝试以下操作:

import re
values = re.findall(r'(\(.*?\))', query) #this would match the '(' and ')' too
values = re.findall(r'\((.*?)\)', query) #if you don't want to capture the braces

正则表达式将通过使用.*找到和之间的模式?使搜索不贪婪。否则,*将匹配到最后的

是否可以发布期望的输出?如果有或在字符串中间,如果查询=插入到用户值1、“PB”、NULL、“用户、示例”中,会怎么样?example@example如果…有一个值,或者中间值,例如查询=插入用户值1,“PB”,NULL,“用户,示例”,example@example',…示例中没有,但我明白你的意思。您可以检查创建的长度列表,如果它正确,您就可以开始了。如果不尝试类似于拆分的方法,我会在过度设计解决方案之前尝试。使用正则表达式通常是反复试验的。太棒了!它就像一个冠军,谢谢,真的很感激太好了@用户202890如果您可以将答案标记为已接受并向上投票,则在答案旁边的复选标记和向上箭头将是超级。谢谢