正则表达式是否可以匹配空格或字符串的开始?正则表达式:匹配开头或空格
我试图取代货币与£符号缩写英镑。我可以匹配任何以英镑开始的东西,但我希望稍微保守些,并寻找周围的某些分隔符。
>>> 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