正則表達式是否可以匹配空格或字符串的開始?正則表達式:匹配開頭或空格
我試圖取代貨幣與£符號縮寫英鎊。我可以匹配任何以英鎊開始的東西,但我希望稍微保守些,並尋找周圍的某些分隔符。
>>> import re
>>> text = u'GBP 5 Off when you spend GBP75.00'
>>> re.sub(ur'GBP([\W\d])', ur'£\g<1>', text) # matches GBP with any prefix
u'\xa3 5 Off when you spend \xa375.00'
>>> re.sub(ur'^GBP([\W\d])', ur'£\g<1>', text) # matches at start only
u'\xa3 5 Off when you spend GBP75.00'
>>> re.sub(ur'(\W)GBP([\W\d])', ur'\g<1>£\g<2>', text) # matches whitespace prefix only
u'GBP 5 Off when you spend \xa375.00'
我可以同時做兩個後面的例子嗎?
這是哪一種語言?它是Perl嗎? – 2009-02-08 13:02:13
Python。 – 2009-02-08 13:03:43
是的,但不管怎樣,概念都是一樣的。 – Mat 2009-02-08 13:14:01