-1
問題
我需要在一個句子(字符串)創建佔位符與隨機數字編號的佔位符。產生句子
例
My son is 6 years old and my dad is 61 years old.
My son is #0 years old and my dad is #1 years old.
這僅僅是一個句子。句子被逐行分開,並且在句子中包含各種位置的數字和(數字位置是混合的和不同的長度)。
我已經嘗試了各種代碼,但其中大多數與模式替換工作。 我知道這個程序,如果我在Python編程方面有更多的經驗,我可以做到。
程序
閱讀句子(文本文件中的行)。計算句子中的數字(d +)的數量,然後開始從句子中的最後一個數字替換爲第一個(#6,#5,#4,...)(如果句子由6個數字組成)。
對於實施例
My dog is 3 years old, 92.4 cm heigh and has 16 teeth.
計數號的數目與各種lenght(1A,1b1,1c11,1111,1.1 8號):4.
1.環(代替第四數):
My dog is 3 years old, 92.4 cm heigh and has #4 teeth.
2.環:
My dog is 3 years old, 92.#3 cm heigh and has #4 teeth.
3.循環:
My dog is 3 years old, #2.#3 cm heigh and has #4 teeth.
4.循環:
My dog is #1 years old, #2.#3 cm heigh and has #4 teeth.
行添加到文件的末尾,並採取從文件中另一條線。 重複這些步驟直到文件結束。
不要在正則表達式做到這一點。也看起來非常像功課,但無論如何,不要用正則表達式來做這件事。 –
它應該是什麼語言?爲什麼循環反向運行? – Bergi
如果您的字符串包含文字#N,例如「兒子1歲6歲」。文字「#1」是否應該被替換/轉義,還是留在? – Moritz