There are cases when you have to use more than one replacement (so
with() won't work),
but you also don't need
Detail details or
any replacement logic, really (so
callback() is a little too much).
You can use
replace()->all()->by()->map() to replace a matched, predefined value (
by its direct counter-part (
Here, we used
replace()->all() as an example, but of course
replace()->only(int) could be used and would replace only the first, or the first few matches.
Normally, had you matched a string that's not present in your map, a
MissingReplacementKeyException would be thrown:
MissingReplacementKeyException is a safe-guard, since
map() is supposed to be only used with a predefined
set of expected matches, and if a different string is matched (like
"xxx"), that's a heads up
that perhaps something went wrong. Either the map was incomplete, or the pattern allowed some unexpected values.
On the other hand, if you want to ignore unexpected values - use
mapIfExists(), superfluous occurrences are left unchanged:
Resolving a replacement based on a whole match however, is both uncommon and unpractical. It's much more elastic to resolve
it based on a specific capturing group, using
Remember, that groups can be matched or not matched, so we need to specify how to
handle an unmatched group. In this case, we'll use
orElseThrow(), since we don't
expect the group to ever be unmatched.