[英]How to replace certain characters after other certain characters in a string, Python?
[英]How to cut after a certain sequence of characters in python
我想获取一个字符串(例如:TestExample123)并在一组特定字符(给出输出 123)之后截断数字,但我不知道我可以这样做的任何方式。 任何帮助都会很棒,但请注意,我对编码还很陌生,因此可能无法做非常先进的东西。
谢谢。
也许不是最好的方法,但尝试使用 RegEx 绝对是一个好的开始。
import re
reg = "\d"
text = "TestExample123"
text_2 = "Test123Example123"
regexed_text = re.sub(reg, "", text))
regexed_text_2 = re.sub(reg, "", text_2))
这给出了regexed_text = "TestExample"
和regexed_text_2 = "TestExample"
re.sub(reg, "", text)
将给定输入字符串变量text
所有reg
正则表达式匹配元素替换为空 ( ""
)
\\d
表示任何数字
查看 RegEx 模式以获取更多信息: rexegg.com 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.