我有以下字符串,我想提取元素 (xx="yy") 以及括号之间的内容。这是一个例子:
[caption id="get this" align="and this" width="and this" caption="and this"]this too please[/caption]
我试过下面的代码,但我对正则表达式还是个菜鸟。
re.sub(r'\[caption id="(.*)" align="(.*)" width="(.*)" caption="(.*)"\](.*)\[\/caption\]', "tokens: %1 %2 %3 %4 %5", self.content, re.IGNORECASE)
提前致谢!
最佳答案
它可能不适合你,因为 .*
是贪婪的。尝试用 [^"]*
代替它。[^"]
表示除引号字符外的所有字符的集合。此外,正如您在评论中指出的那样, token 语法是 \\n
,而不是 %n
。试试这个:
re.sub(r'\[caption id="([^"]*)" align="([^"]*)" width="([^"]*)" caption="([^"]*)"\](.*)\[\/caption\]', "tokens: \\1 \\2 \\3 \\4 \\5", self.content, re.IGNORECASE)
标题标签的内容是否跨越多行?如果他们这样做,.*
将不会捕获换行符。您需要向我们提供类似 [^\x00]*
的内容。 [^\x00]
表示除空字符外的所有字符的集合。
re.sub(r'\[caption id="([^"]*)" align="([^"]*)" width="([^"]*)" caption="([^"]*)"\]([^\x00]*)\[\/caption\]', "tokens: \\1 \\2 \\3 \\4 \\5", self.content, re.IGNORECASE)
如果您的字符串实际上可以合法地包含空字符,则您需要改用 re.DOTALL
标志。
关于python - 提取括号内和括号之间的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7284570/