0

I have a binary string from a stdout in the form of b'This is ___ \n' where the blank can a selection of fixed words, say ['apple', 'orange', 'pie']. I want to assert whether the keyboard match my intended keyboard. One way I can:

s = b'This is Apple \n'

def check(s, correct_answer):
    if correct_answer in str(s).lower():
        return True
    else:
        return False
check(s, 'apple')

What would be the regex way to solve this? I know the keyword can only be one of the 3.

J_yang
  • 2,672
  • 8
  • 32
  • 61
  • `(?:apple|orange|pie)` will match one of the three words, so if you create a regex just paste it where it should be, `r'This is (?:apple|orange|pie) \n'` – Wiktor Stribiżew Apr 30 '20 at 12:48
  • Your's is just a membership check, which does not check if `'apple'` is in the required position of string. – Austin Apr 30 '20 at 12:49
  • @WiktorStribiżew This is not quite yet. (?:apple|orange|pie) Only check whether one of the work is . so if s = 'This is Apple', given 'Banana' as correct_answer should return false. – J_yang Apr 30 '20 at 12:51
  • Yes, it will. Please try, if it fails to work, please update the question and let know. – Wiktor Stribiżew Apr 30 '20 at 12:53

0 Answers0