我有以下文字:
text = "The equation is Ue^(jα)."
当第一个圆括号前面有 ^ 时,我想自动将 ( 括号替换为 { 括号。
所以我尝试了:
text = "The equation is Ue^(jα). Some brackets like those () should stay. If this symbol ^ is alone nothing should happen. "
text = re.sub(r'^((.*?))<',r'^{\1} ', text)
text
……但是好像不行。知道如何让它发挥作用吗?
像 () 这样的括号应该保留。如果此符号 ^ 是单独的,则不会发生任何事情。
请您参考如下方法:
你忘了转义一些字符:
>>> text = "The equation is Ue^(jα). Some brackets like those () should stay. If this symbol ^ is alone nothing should happen. "
>>> re.sub(r'\^\((.*?)\)',r'^{\1} ', text)
'The equation is Ue^{jα} . Some brackets like those () should stay. If this symbol ^ is alone nothing should happen. '
