如何在XSD架构中正确的转义正则表达式模式? [英] How to properly escape Regular Expression pattern in XSD schema?

查看:379
本文介绍了如何在XSD架构中正确的转义正则表达式模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要满足以MM / DD / YYYY形式接受价值的要求。



从我读到的内容: https://www.w3.org/TR/xmlschema11-2/#nt-dateRep
使用

 < xs:simpleType name =DATE> 
< xs:restriction base =xs:date/>
< / xs:simpleType>

不正常,因为其正则表达式显然不支持此格式。



我已经找到并调整了这种格式:

  ^(?:(? :???0 [13578] | 1 [02])(\ /)31)\1 |(:( ?: 0 [1,3-9] | 1 [0-2])(\ / )(?: 29 | 30)\2))(:( ?: 1 [6-9] | [2-9] \d)\d {2})$ |?^(?: 0 ?2(\ /)29\3(:( :( ?: 1 [6-9] |?[2-9] \d)(?:0 [48] | [2468] [048 ] | [13579] [26])|(:( ?: 16 |?[2468] [048] | [3579] [26])00))))$ |?^(:( ?: 0 [1 -9])|(?:λ1 [0-2]))(\ /)(?: 0 [1-9] | 1\d | 2 [0-8])\4(?: (?:1 [6-9] | [2-9] \d)?\d {2})$ 

以下形式:

  \ ^ \(\?:\ ?:\(\:?0\ \ [13578\] \ | 1\ [02\] \)\(\\ / \)31\)\\ \\1\ | \(\:\(\:???0\ \ [1,3-9\] \ | 1\ [0-2\] \) \(\\ / \)\(\:29\ | 30\?)\2\)\)\(\:\(\\ ?:?1\ [6-9\] \ | \ [2-9\] \d\)\ \d {2} \)$ \ | \ ^ \\ \\(\?0\ 2\(\\ / \)29\3\(\:?\(\:\(\?1\ [6-9\] \ | \ [2-9\] \d\)\(\:?0\ [48\] \ | \ [2468 \] \ [048\] \ | \ [13579\] \ [26\] \)\ | \(\:?\(\:16\ | \ [2468\] \ [048\] \ | \ [3579\] \ [26\] \)00\)\)\)\)$ \\ \\ | \ ^ \(\:\(\:0\ \ [1-9\] \)\ | \(\:????1\ [0 -2\] \)\)\(\\ / \)\(\:?0\ \ [1-9\] \ | 1\d\\ \\ | 2\ [0-8\] \)\4\(\:\(\:?1\ [6-9\] \ | \ [2- 9\] \d\)\?\d {2} \)$ 

现在我不再得到XML编辑中的无效转义错误itors(使用XML Spy),但我得到这个:

  invalid-escape:给定的字符转义不被识别。 

我已经按照XML模式规范完成了转义:
https://www.w3.org/TR/xmlschema-2/#regexs Section F .1.1有一个逃生桌。



任何人都可以帮忙解决这个问题吗?



谢谢!

解决方案

如果您检查XSD正则表达式语法资源,你会注意到没有支持 非捕获组 (?:...)),也不是 反向引用 \\\
请参阅捕获组中捕获的文本,(...))。



由于唯一的分隔符ter是 / ,您可以完全摆脱反向引用。



使用


/((0?[13-9] | 1 [0- [13578] | 1 [02])/ 2])/(29 | 30)/))((1 [6-9] | [2-9] \d)\d {2})|?(0 2/29 /(( (1 [6-9] | [2-9] \d)(0 [48] | [2468] [048] | [13579] [26])|((16 | [2468] [048] | [35 79] [26])00))))|(0 [1-9] |λ1 [0-2])/(0 [1-9] |?1\d | 2 [O- 8])/(1 [6-9] | [2-9] \d)?\d {2})

请参阅此正则表达式演示



请注意, regular -expressions.info


特别值得注意的是,完全没有插入符号和美元,字边界和环绕等锚点。 XML模式总是隐式地锚定整个正则表达式。正则表达式必须匹配要被认为有效的元素的整个元素。


所以,你不应该使用<$ c XSD正则表达式中的$ c> ^ (开始的字符串)和 $ (字符串的结尾)



/ 符号以正则表达式转义为正则表达式分隔符,而在XSD正则表达式中,那里没有正则表达式分隔符(因为唯一的操作是匹配,并且没有修饰符: XML模式不提供指定匹配模式的方法 )。所以,不要在XSD正则表达式中转义 /



测试在线测试注意



如果您在 regex101.com 或类似网站,请注意,在大多数情况下,您需要退出 / 如果它被选择为正则表达式分隔符。完成测试后,您可以在 / 之前安全地删除 \


I need to fulfill a requirement to only accept values in the form of MM/DD/YYYY.

From what I've read on: https://www.w3.org/TR/xmlschema11-2/#nt-dateRep Using

<xs:simpleType name="DATE">
        <xs:restriction base="xs:date"/>
    </xs:simpleType>

Is not going to work as its regex apparently is not supporting this format.

I have found and adjusted this format:

^(?:(?:(?:0?[13578]|1[02])(\/)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

To this form:

\^\(\?:\(\?:\(\?:0\?\[13578\]\|1\[02\]\)\(\\/\)31\)\1\|\(\?:\(\?:0\?\[1,3-9\]\|1\[0-2\]\)\(\\/\)\(\?:29\|30\)\2\)\)\(\?:\(\?:1\[6-9\]\|\[2-9\]\d\)\?\d{2}\)$\|\^\(\?:0\?2\(\\/\)29\3\(\?:\(\?:\(\?:1\[6-9\]\|\[2-9\]\d\)\?\(\?:0\[48\]\|\[2468\]\[048\]\|\[13579\]\[26\]\)\|\(\?:\(\?:16\|\[2468\]\[048\]\|\[3579\]\[26\]\)00\)\)\)\)$\|\^\(\?:\(\?:0\?\[1-9\]\)\|\(\?:1\[0-2\]\)\)\(\\/\)\(\?:0\?\[1-9\]\|1\d\|2\[0-8\]\)\4\(\?:\(\?:1\[6-9\]\|\[2-9\]\d\)\?\d{2}\)$

Now I no longer get invalid escaping errors in XML editors (using XML Spy), but I get this one:

invalid-escape: The given character escape is not recognized.

I have done the escape according to the XML schema specifications here: https://www.w3.org/TR/xmlschema-2/#regexs Section F.1.1 there is an escape table.

Can anyone please help to nail this down right?

Thanks!

解决方案

If you check the XSD regex syntax resources, you will notice that there is no support for non-capturing groups ((?:...)), nor backreferences (the \n like entities to refer to the text captured with capturing groups, (...)).

Since the only delimiter is /, you can get rid of the backreference completely.

Use

((((0?[13578]|1[02])/31)/|((0?[13-9]|1[0-2])/(29|30)/))((1[6-9]|[2-9]\d)?\d{2}‌​)|(0?2/29/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[35‌​79][26])00))))|(0?[1-9]|1[0-2])/(0?[1-9]|1\d|2[0-8])/(1[6-9]|[2-9]\d)?\d{2})

See this regex demo

Note that acc. to regular-expressions.info:

Particularly noteworthy is the complete absence of anchors like the caret and dollar, word boundaries, and lookaround. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid.

So, you should not use ^ (start of string) and $ (end of string) in XSD regex.

The / symbol is escaped in regex flavors where it is a regex delimiter, and in XSD regex, there are no regex delimiters (as the only action is matching, and there are no modifiers: XML schemas do not provide a way to specify matching modes). So, do not escape / in XSD regex.

TESTING AT ONLINE TESTERS NOTE

If you test at regex101.com or similar sites, note that in most cases you need to escape the / if it is selected as a regex delimiter. You can safely remove the \ before / after you finished testing.

这篇关于如何在XSD架构中正确的转义正则表达式模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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