-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