Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Formatting_Rawstring - Fatal编程技术网

Python 如何格式化包含不同表达式的原始字符串?

Python 如何格式化包含不同表达式的原始字符串?,python,regex,string,formatting,rawstring,Python,Regex,String,Formatting,Rawstring,比方说,我们想用regex捕捉一些东西,使用rawstring来定义模式,模式中有重复的元素和变量。我们还希望使用format()字符串格式表单。如何做到这一点 import re text = '"""!some text' re.findall(r'"{3}{symbol}some\stext'.format(symbol='!'), text) 但这一行将我们引向一个索引器: # IndexError: tuple index out of range 所以,我的问题是:如果原始字符串

比方说,我们想用regex捕捉一些东西,使用rawstring来定义模式,模式中有重复的元素和变量。我们还希望使用
format()
字符串格式表单。如何做到这一点

import re
text = '"""!some text'
re.findall(r'"{3}{symbol}some\stext'.format(symbol='!'), text)
但这一行将我们引向一个
索引器

# IndexError: tuple index out of range
所以,我的问题是:如果原始字符串中包含格式化大括号表达式和重复大括号表达式,如何格式化它


提前谢谢

用花括号替换花括号

>>> import re
>>> text = '"""!some text'
>>> re.findall(r'"{{3}}{symbol}some\stext'.format(symbol='!'), text)
['"""!some text']
但是,在这种情况下,最好只使用
%
格式。

使用(在Python 3.6中介绍):


只是为了完成。。。如果您碰巧需要一个在花括号内有一个变量的字符串,那么您需要这些变量的三倍。是的,这感觉很愚蠢,但我只是需要一个PyLaTeX项目(
doc.append(r'\command{{{{variable}}}')
appends
'\command{value}'
)。
a = 15
print(fr'Escape is here:\n but still {a}')

# => Escape is here:\n but still 15