简体   繁体   中英

Finding text between two delimiters

I have a string ABC_DEF_EFG_IJK

I need to extract the text between two delimiters, but the condition is is DEF_EFG must be always together.

I have tried using split method, but it does not give right context.

Appreciate your help.

Try this for Python 3+:

letters = 'ABC_DEF_EFG_IJK'

splitted = letters.split('_')
final = f'{splitted[1]}_{splitted[2]}'

print(final)

And you should have:

DEF_EFG

If you use Python 2:

# replace this line
final = f'{splitted[1]}_{splitted[2]}'

#with this line
final = '{}_{}'.format(splitted[1], splitted[2])

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