正则表达式可以匹配空格 或者作为字符串的开头吗?
我想用一个符号代替货币的缩写 GBP。我可以匹配任何以 GBP 开头的东西,但是我想更保守一点,并且在它周围寻找某些分隔符。
>>> 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'
我能同时做后两个示例吗?