Adrian
Adrian

Reputation: 199

Regex match only all given characters with duplicates

I'm struggling with one regex pattern problem and I didn't find any similar thread.

For example, I have following characters:

j, a, n

I want to build a regex pattern which searches only strings with ONLY ALL of these characters but it can contains duplicates of these letters. Result of this should looks like:

jan
jana
anj
naj
najana
...

but shouldn't catch these strings:

ana
jaa
nj
aa
nn
a
n
j
..

I tried with length-specified strings, etc but for now my closest pattern is following:

^[jan]{3,}+$

...where the 3 is count of given characters. But it's still giving me result like aaa or nnn.

Will be thankful for any help.

Upvotes: 1

Views: 95

Answers (1)

Ωmega
Ωmega

Reputation: 43673

^(?=.*j)(?=.*a)(?=.*n)[jan]+$

https://regex101.com/r/FhQfeR/1

Upvotes: 3

Related Questions