There are several ways to read offsets of your matched occurrences and your capturing groups.
If you use
Detail object (like the one passed to
map() callback) you can just use
$detail = pattern('\d+')->match('I was born in 1996')->first();
return 'Match was found at ' . $detail->offset();
'Match was found at 14'
offset() is UTF-8 safe and returns offsets in characters, not bytes.
For bytes, consider using
byteOffset() method. Use
offset() with multibyte-safe methods like
byteOffset() with methods like
$detail = pattern('(?<capital>[A-Z])[a-z]+')->match('my name is John Cena')->first();