File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 6
6
7
7
#计算单词个数
8
8
def 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 ) #修改了正则表达式
10
10
amount = len (words )
11
11
return str (amount )
12
12
@@ -21,7 +21,7 @@ def file_read(filename):
21
21
string = file_read ('GitHub.txt' )
22
22
result = counter (string )
23
23
print 'There are' , result , 'words in this article.'
24
- print " 这篇文章中有" + result + " 个英文单词"
24
+ print ' 这篇文章中有' + result + ' 个英文单词'
25
25
26
26
27
27
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