betweenResult method
Extracts content between start and end delimiters.
Returns a tuple of (content between delimiters, remaining string after removal).
Uses lastIndexOf for the end delimiter to capture the outermost pair,
which correctly handles nested delimiters like (a(test)b) → a(test)b.
Implementation
(String, String?)? betweenResult(
String start,
String end, {
bool endOptional = true,
bool trim = true,
}) {
if (isEmpty || start.isEmpty || end.isEmpty) return null;
final int startIndex = indexOf(start);
if (startIndex == -1) {
return null;
}
final int endIndex = lastIndexOf(end);
if (endIndex == -1) {
return null;
}
if (startIndex >= endIndex || (startIndex + start.length) > endIndex) {
return null;
}
final String found = substringSafe(startIndex + start.length, endIndex);
final String remaining = substringSafe(0, startIndex) + substringSafe(endIndex + end.length);
final String finalFound = trim ? found.trim() : found;
final String finalRemaining = trim
? remaining.replaceAll(RegExp(r'\s+'), ' ').trim()
: remaining;
return (finalFound, finalRemaining.isEmpty ? '' : finalRemaining);
}