首页 > 解决方案 > 如何使用正则表达式拆分此字符串“-25+26+78-21”以获得-25、26、78、-21?

问题描述

如何使用正则表达式拆分此字符串“-25+26+78-21”以获得-25、26、78、-21?

标签: javaregex

解决方案


你可以尝试这样的事情:

    //your input
    String numbers = "-25+26+78-21";
    //split lookahead by + or - and store them in array of strings
    //you can do with it afterwards whatever you like, turn it into ints for example
    String[] tokens = numbers.split("(?=\\-)|\\+");
    System.out.println(Arrays.asList(tokens));

推荐阅读