extractWords function

List<String> extractWords(
  1. String text
)

Implementation

List<String> extractWords(String text) {
  // Replace case transitions (e.g., a lowercase letter followed by an uppercase) with a space
  final separated = text.replaceAllMapped(RegExp(r'([a-z])([A-Z])'), (match) => '${match.group(1)} ${match.group(2)}');

  // Now extract all word-like tokens
  final wordRegExp = RegExp(r'\b\w+\b');
  return wordRegExp.allMatches(separated).map((m) => m.group(0)!).toList();
}