Checksum321
Checksum321

Reputation: 21

Printing first word after keyword

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

Answers (2)

Austin
Austin

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

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions