简体   繁体   English

如何查找字符串是否包含所有特定字符?

[英]How to find if a string contains all the certain characters?

For example: 例如:

Characters to match: 'czk'
string1: 'zack' Matches
string2: 'zak'  Does not match

I tried (c)+(k)+(z) and [ckz] which are obviously wrong. 我试过(c)+(k)+(z)[ckz]这显然是错误的。 I feel this is a simple task, but i am unable to find an answer 我觉得这是一项简单的任务,但我无法找到答案

The most natural way would probably to use sets rather than regex, like so 最自然的方式可能是使用集合而不是正则表达式,就像这样

set('czk').issubset(s)

Code is very often simpler and easier to maintain without using regex much. 在不使用正则表达式的情况下,代码通常更简单,更易于维护。

基本上你必须首先对字符串进行排序,这样你才能得到“ackz”,然后你可以使用像/.*c.*k.*z.*/这样的正则表达式进行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM