Word count. How many words are in a string? To answer, we can develop a Python method, wordcount, that uses re.findall to count words.
Python method info. The wordcount() method locates and counts non-whitespace characters with a special pattern. This allows us to avoid complex character testing in a loop.
Example. The re.findall method is the most important part of this solution. It finds all matches within a string. When we count them, we can count matching patterns.
Note We specify the pattern \S+ in the re.findall method. This means "one or more non-whitespace characters."
import re
def wordcount(value):
# Find all non-whitespace patterns.
list = re.findall(r"(\S+)", value)
# Return length of resulting list.
return len(list)
value = "To be or not to be, that is the question."
print(wordcount(value))
value = "Welcome, friend, to my website!"
print(wordcount(value))
value = ""
print(wordcount(value))10
5
0
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.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 20, 2024 (edit).