不允许在 XSD 正则表达式中使用特定字符串 [英] Not allowing a specific string in an XSD Regular expression

查看:41
本文介绍了不允许在 XSD 正则表达式中使用特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式验证受限字符串...

I'm trying to validate a restricted string using a regular expression ...

<xs:simpleType name="myStringType">
    <xs:restriction base="xs:string">
        <xs:pattern value="^urn:mystuff:v1:(ABC\.(?!Acme).\S+\.\S+\.a\d+\.v\d+|ABC\.Acme\.\S+\.a\d+\.\d+\.\d+)$"/>
    </xs:restriction>
</xs:simpleType>

如您所见,我尝试使用的正则表达式是

As you can see the regular expression I'm trying to use is

^urn:mystuff:v1:(ABC\.(?!Acme).\S+\.\S+\.a\d+\.v\d+|ABC\.Acme\.\S+\.a\d+\.\d+\.\d+)$

我想验证以下内容:

urn:mystuff:v1:ABC.Test.MyData.a1.v1
urn:mystuff:v1:ABC.Acme.MyData.a1.0.1

但我希望以下失败

urn:mystuff:v1:ABC.Acme.MyData.a1.v1

这似乎在 在线正则表达式测试器中运行良好,但是当我使用 Oxygen XML 编辑器时我收到以下错误.

This appears to work fine in an online regex tester but when I use Oxygen XML Editor I get the following error.

 Pattern value '^urn:mystuff:v1:(ABC\.(?!Acme).\S+\.\S+\.a\d+\.v\d+|ABC\.Acme\.\S+\.a\d+\.\d+\.\d+)$' is not a valid regular expression. The reported error was: 'This expression is not supported in the current option setting.'.

这篇文章表明 XSD 正则表达式不支持前瞻和后视,但问题涉及对模式进行编号,因此在示例中采用了蛮力方法.这是可能的,因为可能性的子集非常有限.

This post suggests that lookaheads and lookbehinds are not supported in XSD regex but the question relates to number patterns so a brute force approach is taken in the example. This is possible because there's a very limited subset of possibilities.

当被禁止的值是一个特定的字符串时,如何处理这个问题?

How does one deal with this when the values to be disallowed is a specific string?

推荐答案

附录:请注意,此解决方案在字符串中的固定位置植入了一个伪断言.
对于应该跨越整个字符串的断言的示例解决方案
看到这个问题 不允许特定字符串的 XML 模式限制模式

adendum : Note that this solution plants an pseudo assertion at a fixed location in the string.
For an example solution of an assertion that should span the entire string
see this question XML schema restriction pattern for not allowing specific string

edit : 正如评论中所指出的,使用 (..) 而不是 (?:..) 如果这是只有
支持的构造.
变了!

edit : As pointed out in a comment, use (..) instead of (?:..) if that is the only
supported construct.
Changed !

这个系列(?!Acme)\S+\.可以换成这个大系列:

This series (?!Acme)\S+\. can be replaced with this large series :

([^A]\S*|A([^c.]\S*)?|Ac([^m.]\S*)?|Acm([^e.]\S*)?)\.

哪个更大,但应该涵盖所有情况并立即制作正则表达式:

which is bigger but should cover all cases and makes the regex now :

urn:mystuff:v1:(ABC\.([^A]\S*|A([^c.]\S*)?|Ac([^m.]\S*)?|Acm([^e.]\S*)?)\.\S+\.a\d+\.v\d+|ABC\.Acme\.\S+\.a\d+\.\d+\.\d+)

https://regex101.com/r/qXv9HU/2

扩展

 urn:mystuff:v1:
 (                             # (1 start)
      ABC \. 
      (                             # (2 start)
           [^A]  \S* 
        |  A 
           ( [^c.] \S* )?                # (3)
        |  Ac 
           ( [^m.] \S* )?                # (4)
        |  Acm  
           ( [^e.] \S* )?                # (5)
      )                             # (2 end)
      \. 
      \S+ \. a \d+ \. v \d+ 
   |  
      ABC \. Acme \. \S+ \. a \d+ \. \d+ \. \d+ 
 )                             # (1 end)

这篇关于不允许在 XSD 正则表达式中使用特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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