擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/3.4/howto/regex.html#regex-howto" rel="nofollow">regular expressions</a>来检查文本中的每个单词是否与特定模式匹配。在您的例子中,模式是一个单词中的所有字符都应该是字母表中的字母:大写字母<strong>a-Z</strong>以及小写字母<strong>a-Z</strong>(我从您的例子中假设)和字母<strong>。在</p>
<p>对于初学者来说,学习如何使用正则表达式可能会让人望而生畏,但通过一点练习,你会发现它们非常有用和高效。在</p>
<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
def check_allowed_chars(text):
"""
"""
pattern = re.compile('[a-zA-Zõäöü]+$')
words = text.split()
for word in words:
if not pattern.match(word):
print('The text contains not allowed characters!')
return
</code></pre>