python - 用大括号自动替换圆括号
问题描述
我有以下文字:
text = "The equation is Ue^(jα)."
当第一个圆括号前面有 a 时,我想(
用括号自动替换括号。{
^
所以我尝试了:
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. '
推荐阅读
- javascript - 寻找一种基于另一个项目的 ID 来选择类的方法
- android - Android 自定义 lint 规则未检测到任何问题(对于导入)
- javascript - 函数和箭头 (=>) 函数之间有区别吗
- linux - 如何为 .sh 文件制作 .desktop 文件
- cassandra - 在 Cassandra 中插入多次后出现无效或不支持的协议版本错误
- excel - 尝试根据数据移动整行
- java - JSP 导入中的上下文路径
- c# - 具有标签的 DataRows 的自定义 DataTable
- php - Laravel 中的 AJAX 函数未将表单数据发布到控制器
- java - 在 Intellij 中打开 Nullable 警告