Reputation: 21
I would like to get the first word after the specified keyword 'dataplane'
output = 'set interfaces dataplane dp0p1 vif 2129 description '*** WAN - Global ***''
I would like to get the word dp0p1 that comes after dataplane
Upvotes: 0
Views: 44
Reputation: 26039
Assuming you have no possibility for more than one keyword, assuming dataplane
occurs once, you could use next
here:
output = 'set interfaces dataplane dp0p1 vif 2129 description'
splitted = output.split()
print(next((y for x, y in zip(splitted, splitted[1:]) if x == 'dataplane'), ''))
This prints:
dp0p1
Upvotes: 0
Reputation: 521178
Assuming you wanted to possibly make more than one keyword, assuming dataplane
should occur more than once, you could use re.findall
here:
output = 'set interfaces dataplane dp0p1 vif 2129 description '
matches = re.findall('\\bdataplane (\S+)', output)
print(matches)
This prints:
['dp0p1']
Upvotes: 1