我的 xsd 文件中有以下正则表达式类型:
<xsd:simpleType name="Host">
<xsd:restriction base="xsd:string">
<xsd:pattern
value="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b">
</xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
当通过 xjc 在 ant 中生成时,我收到以下异常:
[xjc] [ERROR] InvalidRegex: Pattern value '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b' is not a valid regular expression. The reported error was: 'This expression is not supported in the current option setting.' at column '2'.
[xjc] line 10 of file:/.../src/META-INF/portscan.xsd
我可以通过将每个反斜杠 () 更改为双反斜杠 (\) 来解决此问题:
<xsd:simpleType name="Host">
<xsd:restriction base="xsd:string">
<xsd:pattern
value="\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b">
</xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
但是,当验证在编组期间运行时,我得到以下异常:
Caused by - class org.xml.sax.SAXParseException: cvc-pattern-valid: Value '80.245.120.45' is not facet-valid with respect to pattern '\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b' for type 'Host'.
显然,双反斜杠 (\\) 是导致验证失败的原因。但是如何编码单个反斜杠以使 xjc 工作?
编辑:
嗯,现在找到了答案,似乎 xjc 正则表达式中不支持“\b”。将它们排除在外解决了问题,它现在生成时没有错误,并且似乎在运行时工作。耶!:)
虽然有人知道这在没有单词边界的情况下是否安全?也许有替代方案?