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

Python 如果在整个字符串中查找特定单词,如何使用正则表达式

Python 如果在整个字符串中查找特定单词,如何使用正则表达式,python,regex,Python,Regex,我的输出是 Select action => test: username, status 1 如果存在“username”字符串,我将尝试执行此操作,然后继续编写代码,否则打印失败并退出 如果我写p.expect('*username*')它会返回错误 raise error, v # invalid expression sre_constants.error: nothing to repeat 我想这就是python中已知的bug 在这种情况下,最好的方法是什么?为什么要使用正

我的输出是

Select action => test: username, status 1
如果存在“username”字符串,我将尝试执行此操作,然后继续编写代码,否则打印失败并退出

如果我写
p.expect('*username*')
它会返回错误

raise error, v # invalid expression
sre_constants.error: nothing to repeat
我想这就是python中已知的bug


在这种情况下,最好的方法是什么?

为什么要使用正则表达式?如果只是查看子字符串是否在字符串中,请使用

if `username` in some_string:
    #exec more code.

为什么要用正则表达式呢?如果只是查看子字符串是否在字符串中,请使用

if `username` in some_string:
    #exec more code.

如果出于某种原因打算使用正则表达式:

import re
output = "Select action => test: username, status 1"

UserNameRegEx = re.search('username',output) #RegEx to search for the string "username" in string "output". Stores as an object.

#if regEx object UserNameRegEx exists (meaning that a match was found)
if match_module_present:        
    username = UserNameRegEx.group(1) #assigns the matched string from regex object to a string variable
    #run more code

如果出于某种原因打算使用正则表达式:

import re
output = "Select action => test: username, status 1"

UserNameRegEx = re.search('username',output) #RegEx to search for the string "username" in string "output". Stores as an object.

#if regEx object UserNameRegEx exists (meaning that a match was found)
if match_module_present:        
    username = UserNameRegEx.group(1) #assigns the matched string from regex object to a string variable
    #run more code

如果你不需要全词搜索,请使用下面答案中的一个。我使用它来帮助我学习和编写正则表达式代码。检查用户名是否存在将很棘手。它可能在拼写和对齐方面有变化,实际上可能是一种颜色,即约翰·布朗的大日子。你会错过的。重要的是案例,不管是第一个还是最后一个,或者两者都有。这类似于解析语言,正则表达式不可能做到这一点。如果您确实需要正则表达式,则需要使用转义方法来查找文本
p.expect(re.escape(userstring))
如果您不需要全词搜索,请使用下面答案中的一个。我使用它来帮助我了解并编写正则表达式代码。检查用户名是否存在将很棘手。它可能在拼写和对齐方面有变化,实际上可能是一种颜色,即约翰·布朗的大日子。你会错过的。重要的是案例,不管是第一个还是最后一个,或者两者都有。这类似于解析语言,正则表达式不可能做到这一点。如果您确实需要正则表达式,则需要使用转义方法来查找文本
p.expect(re.escape(userstring))
问题是我正在运行python脚本并向我的设备发送命令。它会在设备上创建日志文件,我正在尝试查看该日志文件中是否存在用户名。因此,如果log.txt中的用户名可以,则无法使用
if username in[x.strip()for x in open(“log.txt”,“r”).readlines()])
尽管您可能不想这样打开文件,您可以根据需要进行调整。这只是一个随机的例子,我正在运行python脚本并向我的设备发送命令。它会在设备上创建日志文件,我正在尝试查看该日志文件中是否存在用户名。因此,如果log.txt中的用户名可以,则无法使用
if username in[x.strip()for x in open(“log.txt”,“r”).readlines()])
尽管您可能不想这样打开文件,您可以根据需要进行调整。只是一个随机的例子,以适应注释。如果我知道输出字符串,这可能是不可能的。在我的例子中,我正在设备上执行一些用户活动,并尝试读取控制台输出。Python脚本是从我的pc上调用的,并且设备是基于unix的。如果我知道输出字符串,这可能是不可靠的。在我的例子中,我正在设备上执行一些用户活动,并尝试读取控制台输出。Python脚本从我的pc调用,设备基于unix