guessFromString static method
Parses a string for dice rolls and returns a list of dice found.
The string must be in the format of XdY
where X is the amount of dice and Y is the amount of sides,
optionally followed by a modifier in the format of +Z
or -Z
where Z is the modifier value.
Example: 2d6+DEX
would return a list with a single dice with 2 amount, 6 sides and a modifier of DEX.
Implementation
static List<Dice> guessFromString(String str) {
final basicRollPattern =
RegExp(r'\broll([+-][a-z]+)\b', caseSensitive: false);
final dicePattern = RegExp(r'\b\dd\d+\b', caseSensitive: false);
final found = <Dice>[];
final basicRollMatches = basicRollPattern.allMatches(str);
for (final match in basicRollMatches) {
found.add(Dice.fromJson('2d6' + match.group(1)!.toUpperCase()));
}
final diceMatches = dicePattern.allMatches(str);
for (final match in diceMatches) {
found.add(Dice.fromJson(match.input.substring(match.start, match.end)));
}
return found;
}