![](/img/trans.png)
[英]Python Regex - How do I fetch a word after a specific word in a string using python regex?
[英]how to use regex if I am looking for a specific word in entire string
我的輸出是
Select action => test: username, status 1
我正在嘗試執行是否存在“用戶名”字符串,然后繼續執行代碼,否則打印失敗並退出
如果我寫p.expect('*username*')
它返回錯誤
raise error, v # invalid expression
sre_constants.error: nothing to repeat
我猜這是python中的已知錯誤。
在這種情況下,最好的方法是什么?
為什么要使用正則表達式呢? 如果您只是在查看子字符串是否在字符串中,請使用
if `username` in some_string:
#exec more code.
如果出於某種原因打算使用RegEx:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.