匹配带有分隔符的集合中的非重复词
Match non-repetitive words from set with separator
我正在尝试编写一个正则表达式来匹配一组由垂直线分隔的单词,这些单词在字符串中应该只出现一次。例如
mode1|mode2|mode3
但它不应该匹配:
mode1|mode2|mode2
mode1|
mode1|mode1
mode1|any_other_word
到目前为止,我已经写了
^(?:(mode1|mode2|mode3)\|?)*(?!(?:mode1|mode2|mode3))$
我相信它已经足够接近预期的结果了。但是我不能让正则表达式不匹配之前出现的单词,比如:
mode1|mode2|mode3|mode2
由于您正在处理 XSD,因此您应该使用枚举而不是正则表达式模式来描述预期值:
<xs:simpleType name="modeString">
<xs:restriction base="xs:string">
<xs:enumeration value="mode1"/>
<xs:enumeration value="mode2"/>
<xs:enumeration value="mode3"/>
</xs:restriction>
</xs:simpleType>
Adapted from
w3schools.com
(经过编辑删除了我不合格的正则表达式答案并专注于 xsd 枚举的使用)。
我正在尝试编写一个正则表达式来匹配一组由垂直线分隔的单词,这些单词在字符串中应该只出现一次。例如
mode1|mode2|mode3
但它不应该匹配:
mode1|mode2|mode2
mode1|
mode1|mode1
mode1|any_other_word
到目前为止,我已经写了
^(?:(mode1|mode2|mode3)\|?)*(?!(?:mode1|mode2|mode3))$
我相信它已经足够接近预期的结果了。但是我不能让正则表达式不匹配之前出现的单词,比如:
mode1|mode2|mode3|mode2
由于您正在处理 XSD,因此您应该使用枚举而不是正则表达式模式来描述预期值:
<xs:simpleType name="modeString"> <xs:restriction base="xs:string"> <xs:enumeration value="mode1"/> <xs:enumeration value="mode2"/> <xs:enumeration value="mode3"/> </xs:restriction> </xs:simpleType>
Adapted from w3schools.com
(经过编辑删除了我不合格的正则表达式答案并专注于 xsd 枚举的使用)。