-1

I have the following strings :

Each string is a user's answer given via the command line to answer the question which geographies do you want to include in the analysis?

The pattern should be valid only if combine, online or store are present in the string and they are either separated by commas and white spaces (or both).

Below are some examples.

Valid Test Cases:

combine
combine, online, store
store online
online

Invalid Test Cases:

combine, anything else
anything else combine
combine-online

2 Answers2

0

Here's a regex that should do the trick:

>>> import re

>>> cases = '''\
combine
combine, online, store
store online
online
combine, anything else
anything else combine
combine-online
'''.splitlines()

>>> for line in cases:
        if re.match(r'^(combine|online|store)([ ,]+(combine|online|store))*$', line):
            print(line)

That gives this output:

combine
combine, online, store
store online
online
Raymond Hettinger
  • 182,864
  • 54
  • 321
  • 419
0

What about a list of "allowed words" only including a space and comma?

^(?:combine|online|store| |,)+$

Test here

JvdV
  • 41,931
  • 5
  • 24
  • 46