Java 正则表达式与 XSD 正则表达式

Java regex vs XSD regex

我有一个 XSD,其字段具有以下模式值:

<xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{1,6}(/([IXCDVML]+)|([0-9]+))?"/>
</xs:restriction>

如何在Java中写出上面的XSD?

XSD中的正则表达式由^$隐式锚定在开始和结束处,但Java中的正则表达式不是,因此显式添加它们:

^[0-9]{1,6}(/([IXCDVML]+)|([0-9]+))?$

Reference:

Note: Unlike some popular regular expression languages (including those defined by Perl and standard Unix utilities), the regular expression language defined here implicitly anchors all regular expressions at the head and tail, as the most common use of regular expressions in ·pattern· is to match entire literals.