You need two capturing groups before and after a comma (and delimiters):
$regex = "/{loadsegment (\d+),(\d+)}/";
Also, I'm using \d
which is shorthand for a digit, instead of .*?
, which is anything.
I also removed t+
for t
, since t+
will match t
one or more times, which doesn't seem like what you want to do.
To make the second group optional, you'd use the ?
modifier:
/{loadsegment (\d+),(\d+)?}/
But this still requires the comma. You can make it optional as well...
/{loadsegment (\d+),?(\d+)?}/
... but now your regex will match:
{loadsegment 123,}
which we probably dont want. So, we include the comma in the optional second group, like this:
/{loadsegment (\d+)(?:,(\d+))?}/
Explanation (minus the delimiters):
{loadsegment - Match "{loadsegment "
(\d+) - Match one or more digits, store in capturing group 1
(?: - Non-capturing group (so this won't be assigned a capturing group number
, - Match a comma
(\d+) - Match one or more digits, store in capturing group 2
)
? - Make the entire non-capturing group optional
Demo at RegExr