Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一些输入,其中包括如下行:
5feet 23m^2 7 m/s
我想将这些重写为:
5 feet 23 m^2 7 m/s
为此,我可以使用:
re.sub(r"([0-9])(?=[a-zA-Z])",r"\1*","5feet")
但是,我也有如下数字:
23e-7 58.234e-200
与上述模式匹配。
有没有办法让正则表达式以某种方式匹配第一组,但排除第二组?
您可以附加一个否定的前瞻断言(?!...)(与 的相反(?=...))来排除这种情况:
(?!...)
(?=...)
re.sub(r"([0-9])(?=[a-zA-Z])(?!e[+-]?\d)",r"\1*","5feet")