Regular Expression all characters except last one

后端 未结 2 574
无人共我
无人共我 2020-12-14 21:20

This is my string: 50.00.00..00.00

I want to match all . except the last one, so after a replace I end up with 50000000.00

相关标签:
2条回答
  • 2020-12-14 21:36
    \.(?=.*\.)
    

    Matches a dot (\.), which there must be another dot following it ((?=.*\.)).

    (This assumes the regex engine supports lookahead, e.g. PCRE, Python, etc.)

    0 讨论(0)
  • 2020-12-14 21:43

    So you did not specified your regex tools, engine, etc. Well you can do this with e.g. sed (only work if there are always two digits after the last dot and the last dot is always present):

    echo "50.00.00..00.00" | sed 's/\.//;s/\(..\)$/.\1/'
    

    But there are several other ways, e.g. with lookahead regex (if it's supported for you).

    HTH

    0 讨论(0)
提交回复
热议问题