Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 424065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:14:34+00:00 2026-05-12T19:14:34+00:00

I use the following regular expression to validate a comma separated list of values.

  • 0

I use the following regular expression to validate a comma separated list of values.

^Dog|Cat|Bird|Mouse(, (Dog|Cat|Bird|Mouse))*$

The values are also listed in a drop down list in Excel cell validation, so the user can select a single value from the drop down list, or type in multiple values separated by commas.

The regular expression does a good job of preventing the user from entering anything but the approved values, but it doesn’t prevent the user from entering duplicates. For example, the user can enter “Dog” and “Dog, Cat”, but the user can also enter “Dog, Dog”.

Is there any way to prevent duplicates using a similar single regular expression? In other words I need to be able to enforce a discrete list of approved comma separated values.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:14:35+00:00Added an answer on May 12, 2026 at 7:14 pm

    Use a backreference and a negative lookahead:

    ^(Dog|Cat|Bird|Mouse)(, (?!\1)(Dog|Cat|Bird|Mouse))*$
    

    EDIT: This won’t work with cases such as “Cat, Dog, Dog” … You’ll need to come up a hybrid solution for such instances – I don’t believe there is a single regex that can handle that.


    Here’s another technique. You need to check two things, first, that it DOES match this:

    (?:(?:^|, )(Dog|Cat|Bird|Mouse))+$
    

    (That’s just a slightly shorter version of your original regex)

    Then, check that it DOES NOT match this:

    (Dog|Cat|Bird|Mouse).+?\1
    

    E.g.

    var valid = string.match( /(?:(?:^|, )(Dog|Cat|Bird|Mouse))+$/ ) &&
               !string.match( /(Dog|Cat|Bird|Mouse).+?\1/ );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i use the following regular expression to validate a textArea in JSF: ^([a-zA-Z0-9]+[a-zA-Z0-9
The following regular expression is in use to validate email addresses: ^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$ I need
I need to use the following regular expression to validate some Asian characters $regexp
I use the following regular expression to escape special characters: var searchForTest = text.replace(/[^a-zA-Z
I try use string as a regular expression pattern but I've following errors PHP
I am trying to use the following regular expression to check whether a string
I have an issue regarding the use of the following regular expression: private Regex
I have the following regular expression: ^[x]{1}[a-z]{3,4}:[a-z0-9]{1,6} I want to use it to be
I have the following regular expression to validate the date in 'mm/dd/yyyy' format. It
Currently, I use the following regular expression for the user to enter a password

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.