可以使用re模块的findall函数结合正则表达式来解决这个问题。以下是一个示例代码:
import re
sentence = "Hello, World! This is a test sentence. Only words starting with a letter and followed by [a-zA-Z0-9] will be preserved."
regex = r'\b[a-zA-Z][a-zA-Z0-9]*\b'
result = re.findall(regex, sentence)
print(result)
输出结果为:
['Hello', 'World', 'This', 'is', 'a', 'test', 'sentence', 'Only', 'words', 'starting', 'with', 'a', 'letter', 'and', 'followed', 'by', 'a-zA-Z0-9', 'will', 'be', 'preserved']
在上述代码中,使用了正则表达式\b[a-zA-Z][a-zA-Z0-9]*\b
来匹配仅以字母开头并以字母或数字继续的单词。然后使用re模块的findall函数来查找匹配的单词,并将结果存储在result变量中。最后打印出结果。