gpt4 book ai didi

regex - xml 模式检查失败,模式匹配

转载 作者:行者123 更新时间:2023-12-04 09:36:48 24 4
gpt4 key购买 nike

我在 xsd 模式中有这样的样板代码。

      <xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.1"/>
<xs:enumeration value="1.2"/>
<xs:enumeration value="1.3"/>
<xs:enumeration value="1.4"/>
<xs:enumeration value="1.5"/>
<xs:enumeration value="1.6"/>
<xs:enumeration value="1.7"/>
<xs:enumeration value="1.8"/>
<xs:enumeration value="1.9"/>
<xs:enumeration value="1.10"/>
<xs:enumeration value="1.11"/>
<xs:enumeration value="1.12"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
我只想用模式替换如下。
<xs:pattern value="1.[1-9]|[1-1][0-2]"/>
它通过了 1.2 或 1.6,但失败了“1.10”。与 lxml.etree.DocumentInvalid: 元素 'sfd', 属性 'version': [facet 'pattern'] 模式 '1.[1-9]|[1-1][0 不接受值 '1.10' -2]'
我以为 [1-9]|[1-1][0-2] 代表 1-9 和 10-12 之间的范围。
问题是什么?

最佳答案

您的正则表达式(请参阅 its demo )匹配一个字符串

  • 1.[1-9] - 以 1 开头, 然后有除换行符以外的任何字符和来自 1 的数字至 9
  • | - 或
  • [1-1][0-2] - 以 1 开头(注意 [1-1] = 1 )然后是来自 0 的数字至 2 .

  • 您可以使用
    <xs:pattern value="1\.(1[0-2]|[1-9])"/>
    regex demo .请记住,XSD 模式正则表达式模式必须匹配整个字符串,它匹配的内容如下:
  • 1 - 匹配 1
  • \. - 匹配一个文字点( . 没有转义符号匹配任何字符,但换行符字符)
  • (1[0-2]|[1-9]) - 匹配 10 的捕获组(注意 XSD Schema regex 不支持非捕获组) , 11 , 12或来自 1 的数字至 9范围。
  • 关于regex - xml 模式检查失败,模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62537878/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com