![](/img/trans.png)
[英]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.