<分区>
我正在尝试从 Hackerrank 的问题中解决这个问题。这是一个机器学习问题。最初,我试图从语料库文件中读取所有单词以构建单字词频率。根据这个 ML 问题 word
被定义为
Word is a sequence of characters containing only letters from
a
toz
(lowercase only) and can contain hyphens (-
) and apostrophe ('
). Word should begin and end with lowercase letters only.
我用python写了一个正则表达式是这样的:
pat = "[a-z]+( ['-]+[a-z]+ ){0,}"
我尝试同时使用 re.search()
和 re.findall()
。我在这两个方面都有问题。
re.findall()
问题:string = "HELLO W-O-R-L-D"
re.findall()
的输出:[('Hello', ''), ('W', '-D')]
我听不懂
W-O-R-L-D
这个词。在使用re.search()
时,我能够正确地获取它re.search()
问题:string = "123hello456world789"
re.search()
的输出:'hello'
在这种情况下,当使用
re.findall()
时,我可以获得'hello'
和'world'
。