Word made up of exactly 4 different letters using regular expressions?

前端 未结 4 1213
悲哀的现实
悲哀的现实 2021-01-18 10:15

For example reassesses will match. It contains exactly 4 different characters: \'r\', \'e\', \'a\' and \'s\'.

My attempt is: /^([a-z

4条回答
  •  臣服心动
    2021-01-18 11:01

    Something like this:

    ^([a-z])\1*+([a-z])(?:\1|\2)*+([a-z])(?:\1|\2|\3)*+([a-z])(?:\1|\2|\3|\4)*$
    

    The use of possessive quantifiers is essential in this pattern, because it forbids backtracking and avoids that the following capturing group matches a letter that has been found.

    The possessive quantifier feature is available in Java (don't forget to double escape backreferences), but if you need to use the pattern in a language that doesn't have this feature, you can find several options to "translate" the pattern in my comment.

    The above pattern is build to check a whole string, but if you want to find words in a larger string, you can use this (with eventually the case-insensitive option):

    (?

提交回复
热议问题