Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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_String_Substitution - Fatal编程技术网

Python 如何将正则字符串与正则表达式结果合并/联接?

Python 如何将正则字符串与正则表达式结果合并/联接?,python,regex,string,substitution,Python,Regex,String,Substitution,我有一个简单的代码: def modify_mysql_log(): #/var/log/mysql/mysql.log #Change: queries f=open("../mysql/mysql.log") t=open("../mysql/mysql2.log","w") for line in f: line=re.sub(r'Query\s*SELECT[A-Za-z\s+\W_]*\'',random.choice(queri

我有一个简单的代码:

def modify_mysql_log():
    #/var/log/mysql/mysql.log
    #Change: queries
    f=open("../mysql/mysql.log")
    t=open("../mysql/mysql2.log","w")
    for line in f:
        line=re.sub(r'Query\s*SELECT[A-Za-z\s+\W_]*\'',random.choice(queries),line)
        t.write(line)
    f.close()
    t.close()
现在我要做的是,我希望能够用正则表达式替换查询行,正则表达式是我用来搜索的正则表达式,随机选择(查询)与正则表达式结果相结合。我看过其他re函数,但不知怎的,我不知道怎么做

line=re.sub(r'REGEX', random.choice(queries), line)
这段代码将返回通过用random.choice(查询)替换正则表达式的“最左侧非重叠出现”而获得的字符串。如果未找到正则表达式,则返回行


如果我正确理解了这个问题,我会尝试在行中搜索正则表达式并使用string.replace(string_find_by_regex,random.choice(querys)+line)

您是否尝试过字符串格式化函数,如
“%s”(“text”)
“{}”。format(“text”)