Java String.split() sometimes giving blank strings

后端 未结 3 1186
一整个雨季
一整个雨季 2020-11-28 15:25

I\'m making a text based dice roller. It takes in strings like \"2d10+5\" and returns a string as a result of the roll(s). My problem is showing up in the tokenizer that spl

3条回答
  •  野性不改
    2020-11-28 15:50

    I'd recommend simple matching rather than splitting:

    Matcher matcher = Pattern.compile("([1-9]*)(d[0-9%]+)([+-][0-9]+)?").matcher(string);
    if(matcher.matches()) {
        String first = matcher.group(1);
        // etc
    }
    

    No guarantee for the regex, but I think it will do...

提交回复
热议问题