Federico Vicente
Federico Vicente

Reputation: 59

Regex to validate a comma separated list of unique numbers

I am trying to validate a comma separated list of numbers 1-7 unique (not repeating).

i.e.

I tried ^[1-7](?:,[1-7])*$ but it's accepting repeating numbers


var data = [
  '2,4,6,7,1',
  '2,2,6',
  '2',
  '2,',
  '1,2,3,2',
  '1,2,2,3',
  '1,2,3,4,5,6,7,8'
  ];
  
  data.forEach(function(str) {
    document.write(str + ' gives ' + /(?!([1-7])(?:(?!\1).)\1)^((?:^|,)[1-7]){1,7}$/.test(str) + '<br/>');
  });

Upvotes: 2

Views: 1513

Answers (5)

SamWhan
SamWhan

Reputation: 8332

Edit:

Fixed error when repeating digit wasn't the first one.


One way of doing it is:

^(?:(?:^|,)([1-7])(?=(?:,(?!\1)[1-7])*$))+$

It captures a digit and then uses a uses a look-ahead to make sure it doesn't repeats itself.

^                               # Start of line
    (?:                         # Non capturing group
        (?:                     # Non capturing group matching:
            ^                   #  Start of line
            |                   # or
            ,                   #  comma
        )                       #
        ([1-7])                 # Capture digit being between 1 and 7
        (?=                     # Positive look-ahead
            (?:                 # Non capturing group
                ,               # Comma
                (?!\1)[1-7]     # Digit 1-7 **not** being the one captured earlier
            )*                  # Repeat group any number of times
            $                    # Up to end of line
        )                       # End of positive look-ahead
    )+                          # Repeat group (must be present at least once)
$                                # End of line

var data = [
  '2,4,6,7,1',
  '2,2,6',
  '2',
  '2,',
  '1,2,3,4,5,6,7,8',
  '1,2,3,3,6',
  '3,1,5,1,8',
  '3,2,1'
  ];
  
  data.forEach(function(str) {
    document.write(str + ' gives ' + /^(?:(?:^|,)([1-7])(?=(?:,(?!\1)[1-7])*$))+$/.test(str) + '<br/>');
  });

Note! Don't know if performance is an issue, but this does it in almost half the number of steps compared to sln's solution ;)

Upvotes: 0

user557597
user557597

Reputation:

You were pretty close.

 ^                    # BOS
 (?!                  # Validate no dups
      .* 
      ( [1-7] )            # (1)
      .* 
      \1 
 )
 [1-7]                # Unrolled-loop, match 1 to 7 numb's
 (?:
      , 
      [1-7] 
 ){0,6}
 $                    # EOS

var data = [
  '2,4,6,7,1',
  '2,2,6',
  '2',
  '2,',
  '1,2,3,2',
  '1,2,2,3',
  '1,2,3,4,5,6,7,8'
  ];
  
  data.forEach(function(str) {
    document.write(str + ' gives ' + /^(?!.*([1-7]).*\1)[1-7](?:,[1-7]){0,6}$/.test(str) + '<br/>');
  });

Output

2,4,6,7,1 gives true
2,2,6 gives false
2 gives true
2, gives false
1,2,3,2 gives false
1,2,2,3 gives false
1,2,3,4,5,6,7,8 gives false

For a number range that exceeds 1 digit, just add word boundary's around
the capture group and the back reference.
This isolates a complete number.

This particular one is numb range 1-31

 ^                                       # BOS
 (?!                                     # Validate no dups
      .* 
      (                                       # (1 start)
           \b 
           (?: [1-9] | [1-2] \d | 3 [0-1] )        # number range 1-31
           \b 
      )                                       # (1 end)
      .* 
      \b \1 \b 
 )
 (?: [1-9] | [1-2] \d | 3 [0-1] )        # Unrolled-loop, match 1 to 7 numb's
 (?:                                     # in the number range 1-31
      , 
      (?: [1-9] | [1-2] \d | 3 [0-1] )
 ){0,6}
 $                                       # EOS

    var data = [
      '2,4,6,7,1',
      '2,2,6',
      '2,30,16,3',
      '2,',
      '1,2,3,2',
      '1,2,2,3',
      '1,2,3,4,5,6,7,8'
      ];
      
      data.forEach(function(str) {
        document.write(str + ' gives ' + /^(?!.*(\b(?:[1-9]|[1-2]\d|3[0-1])\b).*\b\1\b)(?:[1-9]|[1-2]\d|3[0-1])(?:,(?:[1-9]|[1-2]\d|3[0-1])){0,6}$/.test(str) + '<br/>');
      });

Upvotes: 1

L3viathan
L3viathan

Reputation: 27273

Like other commenters, I recommend you to use something other than regular expressions to solve your problem.

I have a solution, but it is too long to be a valid answer here (answers are limited to 30k characters). My solution is actually a regular expression in the language-theory sense, and is 60616 characters long. I will show you here the code I used to generate the regular expression, it is written in Python, but easily translated in any language you desire. I confirmed that it is working in principle with a smaller example (that uses only the numbers 1 to 3):

^(2(,(3(,1)?|1(,3)?))?|3(,(1(,2)?|2(,1)?))?|1(,(3(,2)?|2(,3)?))?)$

Here's the code used to generate the regex:

def build_regex(chars):
    if len(chars) == 1:
        return list(chars)[0]
    return ('('
    +
    '|'.join('{}(,{})?'.format(c, build_regex(chars - {c})) for c in chars)
    +
    ')')

Call it like this:

'^' + build_regex(set("1234567")) + "$"

The concept is the following:

  • To match a single number a, we can use the simple regex /a/.
  • To match two numbers a and b, we can match the disjunction /(a(,b)?|b(,a)?)/
  • Similarily, to match n numbers, we match the disjunction of all elements, each followed by the optional match for the subset of size n-1 not containing that element.
  • Finally, we wrap the expression in ^...$ in order to match the entire text.

Upvotes: 0

user177800
user177800

Reputation:

No RegEx is needed:

This is much more maintainable and explicit than a convoluted regular expression would be.

function isValid(a) {
  var s = new Set(a);
  s.delete(''); // for the hanging comma case ie:"2,"
  return a.length < 7 && a.length == s.size;
}

var a = '2,4,6,7,1'.split(',');
alert(isValid(a)); // true
a = '2,2,6'.split(',');
alert(isValid(a)); // false
a = '2'.split(',');
alert(isValid(a)); // true
a = '2,'.split(',');
alert(isValid(a)); // false
'1,2,3,4,5,6,7,8'.split(',');
alert(isValid(a)); // false

Upvotes: 2

ishegg
ishegg

Reputation: 9927

Regex are not suited for this. You should split the list into an array and try the different conditions:

function isValid(list) {
    var arrList = list.split(",");
    if (arrList.length > 7) { // if more than 7, there are duplicates
        return false;
    }
    var temp = {};
    for (var i in arrList) {
        if (arrList[i] === "") return false; // if empty element, not valid
        temp[arrList[i]] = "";
    }
    if (Object.keys(temp).length !== arrList.length) { // if they're not of same length, there are duplicates
        return false;
    }
    return true;
}
console.log(isValid("2,4,6,7,1")); // true
console.log(isValid("2,2,6")); // false
console.log(isValid("2")); // true
console.log(isValid("2,")); // false
console.log(isValid("1,2,3,4,5,6,7,8")); // false
console.log(isValid("1,2,3")); // true
console.log(isValid("1,2,3,7,7")); // false

Upvotes: 3

Related Questions