# Find all non-whitespace patterns.
list = re.findall("(\S+)", value)
# Return length of resulting list.
value = "To be or not to be, that is the question."
value = "Stately, plump Buck Mulligan came from the stairhead"
value = ""
Method results. I verified that the method counted correctly the number of words in both (trivial) examples. On more complex samples, such as ones involving markup, results may be less accurate.
So The example method does not count "word endings" but rather the words themselves.
A summary. The regular expression based method for counting words does not exactly mirror all word counting implementations. But in practice the results are close.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2021 (rewrite).