简体   繁体   中英

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

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']

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM