I'm trying to create regex to find all inputs containing max three different characters. It doesn't matter how long the input is.
Example of cases:
I've done regex to find inputs of four or more different chars, but now I need it in opposite way...
(.).*(?\1)(.).*(?\1)(?\2)(.).*(?\1)(?\2)(?\3)(.)
Main question is: How to check number of different characters?
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
The following will match a string with a maximum of three different non-space characters
^\s*(\S)?(?:\s|\1)*(\S)?(?:\s|\1|\2)*(\S)?(?:\s|\1|\2|\3)*$
(\S)
matches one non-space character and captures it so it can then be referenced later in the regex using a back-reference e.g. \1
. The ?
in the (\S)?
are used so the string can contain zero, one, two or three types of non-space characters.
The ?:
make a group non-capturing.
The first part of the regex captures up to three different non-space characters \1
, \2
, \3
, and then (?:\s|\1|\2|\3)*
ensures only those characters or space \s
can then appear before the end of the string $
.
One way, in Javascript, to count the number of different non-space characters in a string "using regex":
var str = 'ABC ABC';
var chars = '';
str.replace( /\S/g, function ( m ) {
if ( chars.indexOf(m) == -1 ) chars += m;
});
chars.length; // 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With