Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check number of different characters using regex?

Tags:

regex

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:

  • "32 32 32 32 34" --> match
  • "MM" --> match
  • " " --> match
  • "1234" --> no match

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?

like image 233
user2335311 Avatar asked Apr 30 '13 10:04

user2335311


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What does regex 0 * 1 * 0 * 1 * Mean?

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.

How do I find special characters in regex?

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).


1 Answers

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
like image 168
MikeM Avatar answered Dec 18 '22 10:12

MikeM