gpt4 book ai didi

python - 在字符串中查找单词的方法

转载 作者:行者123 更新时间:2023-11-28 19:56:16 24 4
gpt4 key购买 nike

我正在搜索一个字符串,将有一行以 ssid 开头,我需要直接在其后查找该词。所以一个例子是“ssid home”,home 是我希望返回的词。我以迂回的方式完成了这件事,看起来很乱,我应该用什么方式来做,也许是正则表达式,或者有没有办法整理我所做的?

a = """
!
interface blah
a
ssid test
v
v
"""

b = a.split("\n")
matches = [x for x in b if "ssid" in x]
matches = [i.split() for i in matches]
print matches[0][1]

最佳答案

a = """
!
interface blah
a
ssid test1
v
ssid test2
v
ssid test3
"""
p = r'(?<=ssid )\S+' # non-whitespace character chunk after ssid
match = re.findall(p, a)

这会给你:['test1', 'test2', 'test3']

关于python - 在字符串中查找单词的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com