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

2024-09-29 01:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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. :(")

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


Tags: 代码文本importreinputsearchmatch字母
1条回答
网友
1楼 · 发布于 2024-09-29 01:30:23

这对我有用,也许你应该试试。你知道吗

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

相关问题 更多 >