Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
REgex for non repeating alphabets comma seperated

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 ?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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,}.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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
about 4 years ago · Juan Pablo Isaza Denunciar

0

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
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda