Check if a string contains only given characters

前端 未结 5 1495
北恋
北恋 2020-12-15 13:51

I\'m wondering if there is more elegant way to check if the string (str = \'abcccbbaabcbca\') contains only \'a\',\'b\' or \'c\' than iterating over it :

for         


        
5条回答
  •  醉酒成梦
    2020-12-15 14:41

    Feel free to mock, since this jumps directly to a regular expression, but test for matching against "^[abc]+$"

    re.match('^[abc]+$', 'string')
    

提交回复
热议问题