Python 正则表达式:如何识别屏幕中的单词(或如何排除标点符号和数字)
有人能帮我识别文本文件中的单词吗?大写或小写,但无数字、括号、破折号、标点符号等(无论“单词”的定义如何) 我在想:Python 正则表达式:如何识别屏幕中的单词(或如何排除标点符号和数字),python,regex,words,Python,Regex,Words,有人能帮我识别文本文件中的单词吗?大写或小写,但无数字、括号、破折号、标点符号等(无论“单词”的定义如何) 我在想: r"\w+ \w+" 但它不起作用 谢谢您可以使用字符类来指定所需字符的范围: r'[a-zA-Z]+' 在这里阅读更多 在python中,可以使用函数re.findall()返回列表中的所有匹配项,或者使用函数re.finditer返回匹配对象的迭代器 re.findall(r"\b[a-z]+\b",test_str,re.I) 你可以这样做 import re tex
r"\w+ \w+"
但它不起作用
谢谢您可以使用字符类来指定所需字符的范围:
r'[a-zA-Z]+'
在这里阅读更多
在python中,可以使用函数re.findall()
返回列表中的所有匹配项,或者使用函数re.finditer
返回匹配对象的迭代器
re.findall(r"\b[a-z]+\b",test_str,re.I)
你可以这样做
import re
text = "hey there 222 how are you ??? fine I hope!"
print re.findall("[a-z]+", subject, re.IGNORECASE)
#['hey', 'there', 'how', 'are', 'you', 'fine', 'I', 'hope']
正则表达式解释
[a-z]+
Options: Case insensitive;
Match a single character in the range between “a” and “z” «[a-z]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Python实时演示
[a-z]+[a-z]+[a-zA-z]+
。当然,这并不完全正确。对于“I love.5 lucy”,out是['l','o','v','e','l','u','c','y',只给出所有正确的字母,但分开。这可以调整为一个单词吗?输出-['love','lucy']。完美,但我想它不能把“我”看成是一个word@TolyI
将在输出中出现抱歉,但它不是TheWordId=r“\b[a-z]+\b”wordI=re.compile(wordID,re.VERBOSE)wordCL=wordI.findall(“我,爱。5:lucy,!”,re.IGNORECASE)