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
Python-将{}与正则表达式之间的子字符串匹配_Python_Regex_String_Match - Fatal编程技术网

Python-将{}与正则表达式之间的子字符串匹配

Python-将{}与正则表达式之间的子字符串匹配,python,regex,string,match,Python,Regex,String,Match,我试图使用正则表达式在两个选择器“{”和“}”之间搜索一个或多个变量子字符串。如果找到多个,则输出应为列表 以下是字符串的示例: mystring = "foofoofoo{something}{anything}foofoofoo" 这是我使用的正则表达式: re.findall(r"^.*(\{.*\}).*$", mystring) 但是它给了我以下输出:{anything} 我试过使用r“(\{.*\})”,它返回我{something}{anything},除了它不是一个列表之外,

我试图使用正则表达式在两个选择器“{”和“}”之间搜索一个或多个变量子字符串。如果找到多个,则输出应为列表

以下是字符串的示例:

mystring = "foofoofoo{something}{anything}foofoofoo"
这是我使用的正则表达式:

re.findall(r"^.*(\{.*\}).*$", mystring)
但是它给了我以下输出:
{anything}

我试过使用
r“(\{.*\})”
,它返回我
{something}{anything}
,除了它不是一个列表之外,这几乎是好的


有什么想法吗?

从正则表达式中移除锚和
*
,让它只捕获
{
}

>>> mystring = "foofoofoo{something}{anything}foofoofoo";
>>> re.findall(r"(\{[^}]*\})", mystring);
['{something}', '{anything}']
要从匹配项中跳过
{
}
,请使用捕获的组:

>>> re.findall(r"\{([^}]*)\}", mystring);
['something', 'anything']

让你的
*
不贪婪。

不需要寻找,只需捕获内容:
{(.*)}
re.findall(r"({.*?})", mystring)