REgex for non repeating alphabets comma seperated

Viewed 111

I have a requirement where I need a regex which

  1. should not repeat alphabet
  2. should only contain alphabet and comma
  3. should not start or end with comma
  4. can contain more than 2 alphabets

example :-

A,B     --- correct  
A,B,C,D,E,F --- correct  
D,D,A   --- wrong  
,B,C    --- wrong  
B,C,    --- wrong  
A,,B,C    --- wrong  

Can anyone help ?

4 Answers

Another idea with capturing and checking by use of a lookahead:

^(?:([A-Z])(?!.*?\1),?\b)+$

You can test here at regex101 if it meets your requirements.

If you don't want to match single characters, e.g. A, change the + quantifier to {2,}.

The statement of the question is incomplete in several respects. I have made the following assumptions:

  1. Considering that D,D,A is incorrect I assume that a letter cannot be followed by a comma followed by the same letter.
  2. The string may contain the same letter more than once as long as #1 is satisfied.
  3. Considering that A,,B,C is incorrect I assume a comma cannot follow a comma.
  4. Since the examples contain only capital letters I will assume that lower-case letters are not permitted (though one need only set the case-indifferent flag (i) to permit either case).

We observe that the requirements are satisfied if and only if the string begins with a capital letter and is followed by a sequence of comma-capital letter pairs, provided that no capital letter is followed by a comma followed by the same letter. We therefore can attempt to match the following regular expression.

^(?:([A-Z]),(?!\1))*[A-Z]$

Demo

The elements of the expression are as follows.

^          # match beginning of string
(?:        # begin a non-capture group
  ([A-Z])  # match a capital letter and save to capture group 1
  ,        # match a comma
  (?!\1)   # use negative lookahead to assert next character is not equal
           # to the content of capture group 1
)*         # end non-capture group and execute it zero or more times
[A-Z]      # match a capital letter
$          # match end of string

Here is a big ugly regex solution:

var inputs = ['A,B', 'D,D,D', ',B,C', 'B,C,', 'A,,B'];
for (var i=0; i < inputs.length; ++i) {
    if (/^(?!.*?([^,]+).*,\1(?:,|$))[^,]+(?:,[^,]+)*$/.test(inputs[i])) {
        console.log(inputs[i] + " => VALID");
    }
    else {
        console.log(inputs[i] + " => INVALID");
    }
}

The regex has two parts to it. It uses a negative lookahead to assert that no two CSV entries ever repeat in the input. Then, it uses a straightforward pattern to match any proper CSV delimited input. Here is an explanation:

^                               from the start of the input
    (?!.*?([^,]+).*,\1(?:,|$))  assert that no CSV element ever repeats
    [^,]+                       then match a CSV element
    (?:,[^,]+)*                 followed by comma and another element, 0 or more times
$                               end of the input

This one could suit your needs:

^(?!,)(?!.*,,)(?!.*(\b[A-Z]+\b).*\1)[A-Z,]+(?<!,)$
  • ^: the start of the string
    • (?!,): should not be directly followed by a comma
    • (?!.*,,): should not be followed by two commas
    • (?!.*(\b[A-Z]+\b).*\1): should not be followed by a value found twice
  • [A-Z,]+: should contain letters and commas only
  • $: the end of the string
    • (?<!,): should not be directly preceded by a comma

See https://regex101.com/r/1kGVSB/1

Related