我们正在使用 JAXB 1.0.6(该项目从 JDK1.4 开始,这是最后一个兼容版本)从 XSD 规范创建 XML 文件。XSD 使用以下正则表达式模式定义属性“电子邮件”。
<xs:simpleType name="EmailAddress">
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="60"/>
<xs:pattern value="([\w%\.\-]+@[\w%\.\-]+\.[a-zA-Z]{2,6})?"/>
</xs:restriction>
</xs:simpleType>
如果您尝试输入电子邮件 bla_bla@somewhere.com,验证将失败并显示以下消息:
attribute "email" has a bad value: the value does not match the
regular expression "([\w%\.\-]+@[\w%\.\-]+\.[a-zA-Z]{2,6})?
恕我直言,字符类 \w 相当于[a-zA-Z0-9_]
所以 bla_bla@somewhere.com 满足表达式。如果省略下划线,则验证将成功通过。为什么会这样?
问候