开发者

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

开发者 https://www.devze.com 2023-04-02 04:13 出处:网络
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. I feel this is a simple task, but i am un

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. I feel this is a simple task, but i am unable to find a开发者_StackOverflow社区n 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.


Basically you have to sort the string first so you get "ackz" and then you can use a regex like /.*c.*k.*z.*/ to match against.

0

精彩评论

暂无评论...
验证码 换一张
取 消