Skip to content

Commit 373170b

Browse files
committed
Fix issue with whitespace and negative operators
1 parent b736491 commit 373170b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

ezdice.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace ezdice;
44

55
class EZDice {
6+
// Magic dice & modifier matching regex
7+
private $re = '(?<operator>[\+-])?\s*(?<number>\d+)(?:[dD](?<sides>(?:\d+|%))(?:-(?<variant>[LlHh]))?)?';
8+
9+
// Stores information on last roll
610
private $total = 0;
711
private $states = [];
812
private $modifier = 0;
@@ -22,8 +26,7 @@ public function roll(string $diceStr)
2226
}
2327

2428
// Search for dice groups and modifiers
25-
$re = '/(?<operator>[\+-])?(?<number>\d+)(?:[dD](?<sides>(?:\d+|%))(?:-(?<variant>[LlHh]))?)?/m';
26-
preg_match_all($re, $diceStr, $matches, PREG_SET_ORDER, 0);
29+
preg_match_all("/{$this->re}/", $diceStr, $matches, PREG_SET_ORDER, 0);
2730

2831
// Returning false if no matches found
2932
if (sizeof($matches) == 0) return false;

0 commit comments

Comments
 (0)