Regex 接受不以特定值开头的5个数字的XSD模式
我想使用正则表达式来查看数字是否以某个值开头。 我的模式应接受01000和98899之间的所有数字,但以977978979981982983984985开头的数字除外 我试过这个:Regex 接受不以特定值开头的5个数字的XSD模式,regex,xsd,Regex,Xsd,我想使用正则表达式来查看数字是否以某个值开头。 我的模式应接受01000和98899之间的所有数字,但以977978979981982983984985开头的数字除外 我试过这个: <xsd:simpleType name="CodeType"> <xsd:restriction base="xsd:integer"> <xsd:pattern value="(?!(977|978|979|981|982|983|984|985))\d{5}
<xsd:simpleType name="CodeType">
<xsd:restriction base="xsd:integer">
<xsd:pattern value="(?!(977|978|979|981|982|983|984|985))\d{5}" />
<xsd:minInclusive value="1000" />
<xsd:maxInclusive value="98899" />
</xsd:restriction>
</xsd:simpleType>
但在XSD模式中似乎不起作用注意XSD模式不支持lookarounds。因此,您必须将图案展开为
([0-8][0-9]{4}|97[0-6][0-9]{2}|98[06-9][0-9]{2}|9[0-69][0-9]{3})
看
由于XSD模式在默认情况下是锚定的,因此我们不应该在两侧使用^
和$
,并且为了避免反斜杠问题,我建议将\d
替换为[0-9]
(它还将使模式匹配普通数字,而不是所有Unicode数字)
这里的所有选项都匹配5位数字
-所有以[0-8][0-9]{4}
开头直到00000
89999
-从97[0-6][0-9]{2}
到97000
97699
-从98[06-9][0-9]{2}
到98000
的整数,不包括不需要的整数(白名单方法)98999
-从9[0-69][0-9]{3}
到90000
和96999
到99000
的整数99999
<xsd:simpleType name="CodeType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1000" />
<xsd:maxInclusive value="98899" />
<xsd:assertion test="every $notAllowedPrefix in ('977','978','979','981','982','983','984','985') satisfies
not(starts-with(string($value), $notAllowedPrefix))"/>
</xsd:restriction>
</xsd:simpleType>
在每个XSD版本中,您都可以使用xs:union在simpleType中允许多个值范围:
<xsd:simpleType name="CodeType">
<xsd:union>
<!-- From 1000 to 97699 -->
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1000" />
<xsd:maxInclusive value="97699" />
</xsd:restriction>
</xsd:simpleType>
<!-- From 98000 to 98199 -->
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="98000" />
<xsd:maxInclusive value="98199" />
</xsd:restriction>
</xsd:simpleType>
<!-- From 98600 to 98999 -->
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="98600" />
<xsd:maxInclusive value="98999" />
</xsd:restriction>
</xsd:simpleType>
</xsd:union>
</xsd:simpleType>
请注意,整数值可以使用不同的文字表示,例如:1000=01000。我的解决方案限制值(它允许1000、01000、00001000等),而使用模式限制文本值。我的想法允许9700和09700,而斯特里比雪夫解决方案只允许09700,但不允许9700。如果使用浮点数,还可以使用1000=01000=1e3=1e3,因此根据具体情况限制值比限制文字更容易维护
此外,请注意,可以在xs:restriction中使用多个xs:pattern,并且simpleType必须至少与其中一个匹配才能有效。因此,您可以通过多种方式使用Stribizev answer:在单个模式中使用完整的正则表达式,或者在多个xs:pattern中使用完整的正则表达式,如果您愿意,或者您可以通过这种方式看得更清楚。您说max是
98800
,但在code中,您有
。看起来应该是98799
。此外,不支持lookarounds。您不能在XSD正则表达式中使用(?!…)
。这只是一个键入错误,我已经纠正了错误。@Stribizev没有work@stribizhev非常好用,谢谢你的努力。非常酷+1。您是手动创建的吗?次要观察:我认为最后一个备选方案中的9[0-69][0-9]{3}
可以减少到9[0-6][0-9]{3}
,因为99xxx被
淘汰了。此外,这也是编写多个xsd:pattern
facet以与漂亮的解释项目并行的一个很好的候选者。干得好@克霍斯:我是手工写的。我一直在用Python和C#编写一个数字范围的正则表达式创建代码,但是排除并不容易,因为它使用反向逻辑。是的,在这种情况下可以减少,我只是更专注于转换匹配所有5位数字(不包括特定数字)的OP regex。也许备选方案的顺序也可以调整。