Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to find word in string

I am searching a string, there will be a line beginning with ssid and I need to find the word directly after that. So an example is "ssid home", home would be the word I wish to return. I have done this in a roundabout way, seems very messy, what way should I do this, a regex perhaps or is there a way to tidy up what I have done?

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]
like image 319
Paul Avatar asked Mar 17 '26 17:03

Paul


1 Answers

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)

This will give you: ['test1', 'test2', 'test3']

like image 81
Ray Avatar answered Mar 20 '26 09:03

Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!