我正在寻找一种最具python风格的方法来替换字符串的第一个和最后一个单词(由于各种原因,以字母为基础进行替换是行不通的)。为了证明我在做什么,这里有一个例子。在
a = "this is the demonstration sentence."
我希望python函数的结果是:
^{pr2}$最棘手的是,在字符串的前面或结尾可能有空格。我需要保存这些。在
我的意思是:
a = " this is a demonstration sentence. "
结果需要:
b = " This is a demonstration Sentence. "
他还想知道正则表达式是否比python的内置方法做得更好,反之亦然。在
这对你有用吗:
下面是一个regex解决方案:
对不起,我只能这么做了。。。在
正则表达式分解:
^{2}$收益率
^{2}$相关问题 更多 >
编程相关推荐