Home > Software engineering >  REgex for non repeating alphabets comma seperated
REgex for non repeating alphabets comma seperated

Time:11-01

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 ?

CodePudding user response:

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");
    }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

CodePudding user response:

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

CodePudding user response:

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