Skip to content

Commit e9c4c50

Browse files
committed
fix 0004.py and add a README file
1 parent 787294c commit e9c4c50

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Jimmy66/0004/0004.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#计算单词个数
88
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) #修改了正则表达式
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

Jimmy66/0004/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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)

0 commit comments

Comments
 (0)