Regular expression: match start or whitespace

前端 未结 8 1629
无人共我
无人共我 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 06:59

    It works in Perl:

    $text = 'GBP 5 off when you spend GBP75';
    $text =~ s/(\W|^)GBP([\W\d])/$1\$$2/g;
    printf "$text\n";
    

    The output is:

    $ 5 off when you spend $75
    

    Note that I stipulated that the match should be global, to get all occurrences.

提交回复
热议问题