vendredi 13 juillet 2018

regex - substring in string

I have this, I guess simple, problem. I'd like to check if substring (in regex format) is part of a string (pb in x). If not it should check if pb_rev is in x else it would print Not found.

import re

x = ('hellopeanutbutterworld')

pb = r'((peanut){1}(butter){1})'
pb_rev = r'((rettub){1}(tunaep){1})'

if pb in x:
    do something
    print('peanut butter found!')
elif pb_rev in x:
    do something
    print('peanut butter reverse found!')
else: print('peanut butter not found :(')

other versions of x:

x = ('dlrowrettubtunaepolleh')
x = ('hellopeanutworld')
x = ('hellobutterworld')

and also if it finds only part of the substring (peanut or butter), then it still should proceed and maybe print where it stopped like print('peanut missing') or print('butter missing').

Thanks! :)

Aucun commentaire:

Enregistrer un commentaire