Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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_Python 2.7_If Statement - Fatal编程技术网

如何在python中使用正则表达式搜索特定单词?

如何在python中使用正则表达式搜索特定单词?,python,regex,python-2.7,if-statement,Python,Regex,Python 2.7,If Statement,我搜索了很多,但我找不到解决问题的方法,我想从输入(文本)中搜索一个特定的单词。虽然我只找到了第一个字母。 我的代码如下: import re regex = r"([a-zA-Z]+)" datm = str(input('Enter a passage to search for a word :')) if re.search(regex, datm): match = re.search(regex, datm) print("Full match: %s" %

我搜索了很多,但我找不到解决问题的方法,我想从输入(文本)中搜索一个特定的单词。虽然我只找到了第一个字母。 我的代码如下:

import re

regex = r"([a-zA-Z]+)"
datm = str(input('Enter  a passage to search for a word :'))
if re.search(regex, datm):

    match = re.search(regex, datm)

    print("Full match: %s" % (match.group(0)))

else:

    print("The regex pattern does not match. :(")

谢谢你的回答,如果你要给负数,你不必回答。

这对我很有效,也许你应该试试

import re
xx = str(input('Enter your text '))
richWord1 = re.findall(r"^\w+",xx) #finding the first word
print (richWord1)

\b语法单词\b
(在此之前,选中
str.split
(在
str.strip
的帮助下)就足够了)。您是否正在尝试在某些给定文本中搜索单词?现在看起来您只是在检查用户是否输入了一个单词。如果你想搜索一些文本,你会从哪里得到这些文本?一些不聪明的人会给出负数,这会妨碍我提问和学习,尽管我警告过他们。我需要的是帮助,而不是负。把你的问题说清楚,回答清楚,你会得到更多的选票。根本不清楚你的问题是什么。你想匹配的单词中有什么特别之处?你为什么认为你需要正则表达式呢?也许你所需要的就是
datm=str(输入('输入一段文字来搜索一个单词:')
,然后
print(datm)
。如果它们按顺序出现,那么这个
[a-zA-Z]+
将匹配多个字母。