我有以下字符串:Python的更換有一定的可變部分子
Billy got score of 2 and Tommy got score of 3
我想拆就score of <some number>
讓我得到
["Billy got","Tommy got"]
我怎樣才能在Python做這種分裂?我試過
input.split("score of \d")
但這不起作用。但是,如果我做
input.split("score of")
然後我得到
["Billy got "," 2 and Tommy got "," 3"]
更新:
感謝您的回答原來的崗位。我有一個後續。
如果我想用score of 2$
替換score of 2
怎麼辦?這意味着每當我看到score of <some number>
只是後面加一個字符$
參見['re.split'(https://docs.python.org/3/library/re.html#re.split)如果你想與分裂一個正則表達式。 – khelwood