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.
这些值也列在Excel单元格验证的下拉列表中,因此用户可以从下拉列表中选择单个值,或键入以逗号分隔的多个值。
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".
正则表达式可以很好地防止用户输入除批准值之外的任何内容,但它不会阻止用户输入重复项。例如,用户可以输入“Dog”和“Dog,Cat”,但用户也可以输入“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!
谢谢!
2 个解决方案
#1
10
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/ );
#2
0
J-P, I tried editing your sample regular expressions so that I could look for duplicates in any comma separated string. Something like this:
J-P,我尝试编辑你的样本正则表达式,这样我就可以在任何逗号分隔的字符串中查找重复项。像这样的东西:
var valid = string.match( /(?:(?:^|, )([a-z]*))+$/ ) &&
!string.match( /([a-z]*).+?\1/ );
Unfortunately, I failed. The Force is weak with me. ;)
不幸的是,我失败了。部队对我很弱。 ;)
Thanks again for your help.
再次感谢你的帮助。
#1
10
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/ );
#2
0
J-P, I tried editing your sample regular expressions so that I could look for duplicates in any comma separated string. Something like this:
J-P,我尝试编辑你的样本正则表达式,这样我就可以在任何逗号分隔的字符串中查找重复项。像这样的东西:
var valid = string.match( /(?:(?:^|, )([a-z]*))+$/ ) &&
!string.match( /([a-z]*).+?\1/ );
Unfortunately, I failed. The Force is weak with me. ;)
不幸的是,我失败了。部队对我很弱。 ;)
Thanks again for your help.
再次感谢你的帮助。