我爲不恰當的主題標題道歉,但我不知道如何定義它。取代python中的部分字符串2.7
我有一個字符串,它看起來像如下:
string = 'b<3 and d >4 or 4<=a <20'
我想改變這種上串入下列操作之一:
new_string = 'b[n]<3 and d[n] >4 or 4<=a[n] <20'
使用功能與string.replace似乎是不成功(例如:string.replace('d','d [n]'),因爲字符串的其他部分也被修改過了(例如結果:'b < 3和[n] d [n]> 4或4 < = a < 20'))。 我想知道python的're'模塊是否適合這份工作?
我正在使用python 2.7。 謝謝。
感謝您的快速回復falsetru。 你會推薦一些關於python的're'模塊的初學者友好教程嗎? 我看了一下: https://docs.python.org/2/library/re.html, https://docs.python.org/2/howto/regex.html, http:///www.tutorialspoint.com/python/python_reg_expressions.htm, ,但所有這些都不是初學者友好的。 – marco 2014-12-07 01:10:53
@marco,其實https://docs.python.org/2/howto/regex.html是我第一次接觸正則表達式。 – falsetru 2014-12-07 01:12:03
謝謝。所以我沒有希望開始學習它。 – marco 2014-12-07 01:14:19