Java 正则表达式与 XSD 正则表达式 [英] Java regex vs XSD regex

查看:111
本文介绍了Java 正则表达式与 XSD 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an XSD with a field that has the following pattern value:

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

如何用Java编写上述XSD?

How can I write the above XSD in Java?

推荐答案

XSD 中的正则表达式在开始和结束时由 ^$ 隐式锚定,但正则表达式在 Java 中不是,所以明确添加它们:

Regular expressions in XSD are implicitly anchored at start and end by ^ and $, but regular expressions in Java are not, so add them explicitly:

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

参考:

注意:与一些流行的正则表达式语言(包括Perl 和标准 Unix 实用程序定义的那些),常规此处定义的表达式语言隐式锚定所有正则头部和尾部的表达式,作为正则最常见的用法·pattern·中的表达式是匹配整个文字.

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.

这篇关于Java 正则表达式与 XSD 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆