how to reduce complexity in regex?

前端 未结 3 1483
面向向阳花
面向向阳花 2021-01-25 23:29

I have a regex which finds all kind of money denoted in dollars,like $290,USD240,$234.45,234.5$,234.6usd

(\\$)[0-9]+\\.?([0-9]         


        
3条回答
  •  长发绾君心
    2021-01-25 23:54

    Reducing the complexity you are reducing the correctness. The following regex works correctly, but even it doesn't take lowcase. (but that could be managed by a key). All other current answers here simply haven't the correct substring for the decimal number.

    ^\s*(?:(?:(?:-?(?:usd|\$)|(?:usd|\$)-)(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d)))|(?:-?(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d))(?:usd|\$)))\s*$
    

    Look here at the test results.

    Make a correct line and only after that try to shorten it.

提交回复
热议问题