简体   繁体   English

当正则表达式匹配时,Python 的 Re.Sub 没有任何改变

[英]Python's Re.Sub Changes Nothing When Regex Matches

Regex confuses me massively and I'm trying to get Python (3.4.0) to replace multiple lines of MD/CSS code (between two lines marking the start and end of the "table" segment) in two instances for a reddit bot.正则表达式让我非常困惑,我正试图让 Python (3.4.0) 在 reddit 机器人的两个实例中替换多行 MD/CSS 代码(在标记“表”段的开始和结束的两行之间)。 It doesn't work in either case and I've tried multiple different regexes for this.它在任何一种情况下都不起作用,为此我尝试了多种不同的正则表达式。 I've also attempted to make it a raw string and escaping more characters (albeit, not tried many combinations), as suggested in some other SO threads.正如其他一些 SO 线程中所建议的那样,我还尝试将其设为原始字符串并转义更多字符(尽管没有尝试过很多组合)。 Annoyingly, the regex matches fine on both regex101.com (on both php and python flavours) and on Pythex.org.令人讨厌的是,regex 在 regex101.com(php 和 python 版本)和 Pythex.org 上都匹配得很好。 Just doesn't work in Python.只是在 Python 中不起作用。

This is the relevant bit of code, both doing more or less the same thing.这是相关的代码位,两者或多或少都在做同样的事情。

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)

I've uploaded the various variables to pastebin.我已将各种变量上传到 pastebin。 The sidebar string is available here , md here , stylesheet here and css here .侧边栏字符串在这里可用,md在这里可用,样式表在这里可用,css在这里可用。

Many thanks for your help.非常感谢您的帮助。

I fixed your regex by compiling it with flag re.DOTALL , to make .我通过用标志re.DOTALL编译它来修复你的正则表达式,使. match newline.匹配换行符。 I also removed escaping from \\n .我也从\\n删除了转义。 Here's modified regular expression:这是修改后的正则表达式:

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

But, if pattern occurs in content only once, I wouldn't bother with so complicates regexes, I'd use str.split() method instead.但是,如果模式只在内容中出现一次,我不会理会如此复杂的正则表达式,我会改用str.split()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM