Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/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';当正则表达式匹配时,s Re.Sub不更改任何内容_Python_Regex - Fatal编程技术网

Python';当正则表达式匹配时,s Re.Sub不更改任何内容

Python';当正则表达式匹配时,s Re.Sub不更改任何内容,python,regex,Python,Regex,Regex让我非常困惑,我试图让Python(3.4.0)在reddit机器人的两个实例中替换多行MD/CSS代码(在标记“表”段开始和结束的两行之间)。这两种情况都不起作用,为此我尝试了多个不同的正则表达式。我还尝试将它变成一个原始字符串,并转义更多的字符(尽管没有尝试过很多组合),正如在其他一些SO线程中所建议的那样。令人烦恼的是,regex在regex101.com(php和python版本)和Pythex.org上都很匹配。只是在Python中不起作用 这是相关的代码位,两者做的或多或少

Regex让我非常困惑,我试图让Python(3.4.0)在reddit机器人的两个实例中替换多行MD/CSS代码(在标记“表”段开始和结束的两行之间)。这两种情况都不起作用,为此我尝试了多个不同的正则表达式。我还尝试将它变成一个原始字符串,并转义更多的字符(尽管没有尝试过很多组合),正如在其他一些SO线程中所建议的那样。令人烦恼的是,regex在regex101.com(php和python版本)和Pythex.org上都很匹配。只是在Python中不起作用

这是相关的代码位,两者做的或多或少是相同的事情

sidebar = r.get_settings(sub)["description"]
regex = r'(?<=\[\]\(#STARTTABLE\)\\n).*?(?=\\n\[\]\(#ENDTABLE\)|$)'
sidebar = re.sub(regex,md,sidebar)
r.update_settings(r.get_subreddit(sub),description=sidebar)


stylesheet = r.get_stylesheet(sub)["stylesheet"]
regex = r'(?<=\/\*START TABLE\*\/).*?(?=\/\*END TABLE\*\/|$)'
stylesheet = re.sub(regex,css, stylesheet)
r.set_stylesheet(sub,stylesheet)
sidebar=r.get_设置(sub)[“description”]
regex=r'(?我用flag修复了您的regex,使
匹配换行符。我还删除了
\n
中的转义。下面是修改后的正则表达式:

regex = re.compile(r'(?<=\[\]\(#STARTTABLE\)\n).*?(?=\n\[\]\(#ENDTABLE\)|$)', re.S)
sidebar = regex.sub(md, sidebar)

regex=re.compile(r'(?不要链接到大的外部文件,请在您的帖子中提供一个小的、自包含的示例来演示这个问题。非常感谢您的帮助:)工作得很好,我不敢相信我从来没有想过将其拆分为3然后重新加入。是的,拆分是一种老方法(PHP 3.0倍),在简单的用例中效果很好。我很高兴能提供帮助:)