如何捕获大括号之间的文本,并在大括号内使用特定模式

时间:2019-05-23 10:36:10

标签: python regex

如果部分字符匹配,我试图捕获大括号(包括大括号)之间的字符。

我已经尝试过下面的正则表达式模式,但是它捕获了从整个字符串的第一个大括号和最后一个大括号开始的所有内容。

<style> * { font-family: "arial" }</style> 

<h1 class="small" style="background-attachment: scroll; background-clip: border-box; background-color: rgb(229, 229, 229); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: repeat; background-size: auto; box-sizing: border-box; color: rgb(229, 229, 229); font-family: inherit; font-size: 22px; font-style: normal; font-variant: normal; font-weight: 600; letter-spacing: normal; line-height: 1.1; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; orphans: 2; padding-bottom: 15px; padding-left: 10px; padding-right: 10px; padding-top: 15px; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"></h1><span style="display: inline !important; float: none; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">
</span><div class="field" style="background-attachment: scroll; background-clip: border-box; background-color: rgb(229, 229, 229); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: repeat; background-size: auto; border-bottom-color: rgb(229, 229, 229); border-bottom-style: solid; border-bottom-width: 1px; border-image-outset: 0; border-image-repeat: stretch; border-image-slice: 100%; border-image-source: none; border-image-width: 1; border-left-color: rgb(229, 229, 229); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(229, 229, 229); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(229, 229, 229); border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: rgb(255, 255, 255); font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 0px; margin-left: auto; margin-right: auto; margin-top: 0px; orphans: 2; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"><br></div

我想捕获“ {list:a,b和c}”,但我却得到“ {name_of_list}列表包含{list:a,b和c}”。

1 个答案:

答案 0 :(得分:1)

.模式匹配任何字符,但换行符不匹配,这就是为什么您得到意外结果的原因。

要使其符合您的需要,您需要“调节”点,在这里,最好使用否定的字符类[^{](除{以外的任何字符)或{{ 1}}(除[^{}]{以外的任何字符):

}

请参见regex demoPython demo

要匹配包含import re s = "The {name_of_list} list contains {list:a, b, and c}. This list should be formatted as a, b, and c." print(re.findall(r'\{[^{}]*?:a, b, and c}', s)) 的花括号内的所有字符串,可以使用

:

请参阅第一个否定字符类中包含的r'\{[^{}:]*:[^{}]*}' ,让我们可以使用贪婪的:量词来使其更有效。