removeAllWordsStartingWithNumber method

String removeAllWordsStartingWithNumber()

Removes all words that begin with a number (e.g., '3rd', '10users', '4u').

This is often used for cleaning up raw input that mixes labels and text. Multiple spaces resulting from the removal are reduced to a single space, and leading/trailing spaces are trimmed.

@returns A new string with number-starting words and extra spaces removed.

Example:

final input = '10users 3rd place is here 54.';
print(input.removeAllWordsStartingWithNumber()); // "place is here 54."

Implementation

String removeAllWordsStartingWithNumber() =>
    replaceAll(RegExp(r'\b\d+\w+\b'), '')
        .replaceAll(RegExp(r'\s{2,}'), ' ')
        .trim();