gpt4 book ai didi

Python3 正则表达式 : Remove all characters except/and | from string

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:01 25 4
gpt4 key购买 nike

我正在用 Python3 编写一个程序,我在一个变量中有一个字符串。此字符串包含大量 |/ 字符,以及可能的字母数字字符和空格。

我需要一个正则表达式来删除除|/ 之外的所有字符,并用空字符替换它们。

所以基本上,/|||/blah/|||||/foo/|/bar/|||/ 会变成 /|||//||| ||//|//|||/.

谁能帮我做这个?提前致谢

最佳答案

上面的也可以简写为re.sub('[^/|]'),即不需要对这两个字符进行转义。

关于Python3 正则表达式 : Remove all characters except/and | from string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com