Regular expression: match start or whitespace

前端 未结 8 1611
无人共我
无人共我 2020-11-29 06:32

Can a regular expression match whitespace or the start of a string?

I\'m trying to replace currency the abbreviation GBP with a £ symbol. I

8条回答
  •  一整个雨季
    2020-11-29 07:02

    This replaces GBP if it's preceded by the start of a string or a word boundary (which the start of a string already is), and after GBP comes a numeric value or a word boundary:

    re.sub(u'\bGBP(?=\b|\d)', u'£', text)
    

    This removes the need for any unnecessary backreferencing by using a lookahead. Inclusive enough?

提交回复
热议问题