File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66
77#计算单词个数
88def counter (string ):
9- words = re .findall (r'[a-zA-Z]+\b ' ,string )
9+ words = re .findall (r'[a-zA-Z]+(\'[a-zA-Z]+|\b) ' ,string ) #修改了正则表达式
1010 amount = len (words )
1111 return str (amount )
1212
@@ -21,7 +21,7 @@ def file_read(filename):
2121 string = file_read ('GitHub.txt' )
2222 result = counter (string )
2323 print 'There are' , result , 'words in this article.'
24- print " 这篇文章中有" + result + " 个英文单词"
24+ print ' 这篇文章中有' + result + ' 个英文单词'
2525
2626
2727
Original file line number Diff line number Diff line change 1+ Some mention about a detail
2+ ===
3+
4+ If you regard 'They're' as two words and don't need to find numbers, just use ` [a-zA-Z]+\b ` is enough.
5+
6+ Well, the way to distinguish ' from ` \b ` that I think up is to write re like this: ` [a-zA-Z]+('[a-zA-Z]+|\b) `
7+
8+ The order is very important, if you write ` [a-zA-Z] ` after ` | ` , it will be ignore.
9+
10+ What's more, in python use ` ' ` in a string ` \' ` is necessary.
11+
12+ re.findall(r'[a-zA-Z]+(\'[a-zA-Z]+|\b)',string)
You can’t perform that action at this time.
0 commit comments