Home > Blockchain >  REGEX for non repeating character(Accepting all languages like Spanish, French, German), No space an
REGEX for non repeating character(Accepting all languages like Spanish, French, German), No space an

Time:08-30

Used in Angular Forms, I have form in UI where I need to add letters, However it should not contain repeat characters and leading trailing commas and No Spaces. Explanation:

I need regex which will not allow

  1. Space
  2. Leading Trailing Commas
  3. Repeat Characters.

For Example:

A,B,C,A - Not Acceptable

A, B,C,D - Not Acceptable

A,B,C,D, - Not Acceptable

,A,B,C,D - Not Acceptable

Á,B,Á - Not Acceptable

A,E,I,O,U,Y,À,È,Ù,É,Â,Ê,Î,Ô,Û,Ë,Ï,Ü,Ÿ,Æ,Œ,Ç - Should be accepted

A,E,U,O,I,Ä,Ü - Should be Accepted

Á,Â,Ã,Ú,Í,E,Ê,É,Ó,Ô,O - Should be accepted

Currently I have one Regex which was answered, but only works for English Characters

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

I tried below regex , but it is not working.

^(?:([A-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆ挜])(?!.*?\1),?\b) $

^(?:([\p{L}])(?!.*?\1),?\b) $

I might be wrong about the above regex, as I am new to Regex,

Can anyone help me with a Regex which can work for other languages as well.

Thank you.

CodePudding user response:

Your regex is very close, just need a small modification:

^(?:([\p{L}])(?!.*?\1)(?:,(?!$)|$)) $

There are no word boundaries between a comma and a non-alphanumeric character(ÀÈÌÒÙ etc.). So your regex didn't work.

Instead you can test (?:,(?!$)|$) for separators: a comma that not followed by the end of a string or the end of a string.

See the test cases here.

CodePudding user response:

You need to use

/^(?!.*(\p{L}).*?\1)\p{L}(?:,\p{L})*$/u

See the regex demo

Details:

  • ^ - start of string
  • (?!.*(\p{L}).*?\1) - no repeated letter anywhere in the string
  • \p{L} - a letter
  • (?:,\p{L})* - zero or more repetitions of a comma and a letter
  • $ - till the end of string.
  • Related