在python中用相同的字符串替换前导文本字符串

2024-04-18 13:48:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个xml文件中有以下标记

&\hbox{(1b)}}$$ with the initial condition <2inline-formula>$x(0)$, where the subscript <3inline-formula>$p$ means 'plant’; <4inline-formula>$x_{p}(t) \in \Re^{n}$ is the state, <5inline-formula>$y_{p}(t)\in\Re^{q}$ is the output, and <6inline-formula>$u_{p}(t)\in\Re^{m}$ is the input; <7inline-formula>

我想将所有以数字开头的内联公式替换为<inline-formula>,但我无法给出搜索以数字开头的内联公式的条件,因此对此有任何帮助。。提前谢谢


Tags: 文件thein标记reiswith数字
1条回答
网友
1楼 · 发布于 2024-04-18 13:48:37

必须使用正则表达式(re模块)来检测“inline formula”(\d+inline-formula)前面的一个或多个数字。你知道吗

例如:

>>> import re
>>> original = "&\hbox{(1b)}}$$ with the initial condition <2inline-formula>$x(0)$, where the subscript <3inline-formula>$p$ means 'plant’; <4inline-formula>$x_{p}(t) \in \Re^{n}$ is the state, <5inline-formula>$y_{p}(t)\in\Re^{q}$ is the output, and <6inline-formula>$u_{p}(t)\in\Re^{m}$ is the input; <7inline-formula>"
>>> new = re.sub(r"<\d+inline-formula>", "<inline-formula>", original)
>>> print new
"&\\hbox{(1b)}}$$ with the initial condition <inline-formula>$x(0)$, where the subscript <inline-formula>$p$ means 'plant\xe2\x80\x99; <inline-formula>$x_{p}(t) \\in \\Re^{n}$ is the state, <inline-formula>$y_{p}(t)\\in\\Re^{q}$ is the output, and <inline-formula>$u_{p}(t)\\in\\Re^{m}$ is the input; <inline-formula>"
>>> 

相关问题 更多 >