Counting all words that start with a capital letter in a string using python dictionary

Hi,

I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key.

#!/usr/bin/env python
import sys
import re
hash = {} # initialize an empty dictinonary
for line in sys.stdin.readlines():
    for word in line.strip().split(): # removing newline char at the end of the line
        #l = re.search("\b[A-Z]\S+", word)
        if word[0].isupper():
            if word in hash:
                hash[word] += 1
            else:
                hash[word] = 1
for word, cnt in hash.iteritems(): # iterating over the dictionary items
    sys.stdout.write("%d %s\n" % (cnt, word))

In the above code, I am using the array index to check for the uppercase. But when I tried to re-code it using the regular expression as:

l = re.search("\b[A-Z]\S+", word)

that is not fetching me the anticipated output. Expecting your help in this regard.

Use raw string notation for regex:

l = re.search(r"\b[A-Z]\S+", word)

And just out of curiosity, any particular reason why you want to go for the regex method, as against continuing with python's excellent string processing capabilities. Avoiding regex means, you don't have to import an extra module.

1 Like