Pattern is not splitting as desired, fails to split by +

前端 未结 2 649
猫巷女王i
猫巷女王i 2021-01-23 20:30

I have the following code :

Pattern pattern = Pattern.compile(\"\\\\d+(?:\\\\.\\\\d+)?(\\\\s\\\\d+(?:\\\\.\\\\d+)?)*\\\\s*[-\\\\+\\\\*/\\\\$£]\");

String input          


        
相关标签:
2条回答
  • 2021-01-23 21:06

    Maybe I am missing something, but from what you describe, I don't see why it needs to be so complicated. "[\\d. ]+?[-+/*£$]" seems to do exactly what you want.

    0 讨论(0)
  • 2021-01-23 21:22

    You can use this regex:

    ((?:\d+(?:\.\d+)?(?:\s+\d+(?:\.\d+)?)*)?\s*[-+*/$£])
    

    RegEx Demo

    In Java it would be:

    Pattern pattern = Pattern.compile
      ( "((?:\\d+(?:\\.\\d+)?(?:\\s+\\d+(?:\\.\\d+)?)*)?\\s*[-+*/$£])" );
    
    0 讨论(0)
提交回复
热议问题