Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:如何识别屏幕中的单词(或如何排除标点符号和数字)_Python_Regex_Words - Fatal编程技术网

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@Toly
I
将在输出中出现抱歉,但它不是TheWordId=r“\b[a-z]+\b”wordI=re.compile(wordID,re.VERBOSE)wordCL=wordI.findall(“我,爱。5:lucy,!”,re.IGNORECASE)