html(s) xsd 文件中的正则表达式验证

html(s) regex validation in xsd file

我已经为我的 xml 模式编写了正则表达式来验证 url(它必须需要 http(s)):

^[https?]+://([^/:]+\.[a-z]{2,10}|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(\/.*)?$

我现在正试图避免来自 https://www.regular-expressions.info/xml.html 的所有限制。我想到了这个:

[https?]+://([^/:]+\.[a-z]{2,10}|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?[^>](\/.*)

但仍然收到关于无效正则表达式的 XMLSchemaParseError。你能帮我解决这个问题吗?

删除 ^$ 锚点是正确的步骤,因为 XmlSchema regex 旨在匹配 整个字符串输入 ,并且 ^$ 不被视为字符串元字符的 start/end,就像在大多数(如果不是全部)其他正则表达式中一样。

匹配1个或多个htps?字符的[https?]+必须转为https?匹配http,一个可选的s

不需要转义/,把\/转成/

另外,最后的/.*应该是可选的,添加?量词。

使用

https?://([^/:]+\.[a-zA-Z]{2,10}|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(/.*)? 

一些用法:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 
  <xsd:element name="ElementNameHere">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="https?://([^/:]+\.[a-zA-Z]{2,10}|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(/.*)? "/> 
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>