在 xsd (C#) 中计算与正则表达式匹配的字符串的最大可能长度 [英] Calculating the maximum possible length for a string matching a regular expression in xsd (C#)

查看:26
本文介绍了在 xsd (C#) 中计算与正则表达式匹配的字符串的最大可能长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSD,我可以从 XSD 以字符串形式访问模式.一个例子:

I am working with XSDs, and I have access to the pattern from the XSD as a string. An example:

<xsd:pattern value="[0-9]{8}"/>

给我 C# 字符串

string pattern = "[0-9]{8}";

从我从 XSD 获得的模式字符串(可以是任何有效的 XSD 模式条目),我试图计算出 XML 中字段内容的最大可能长度.

From the pattern string I obtain from the XSD (which could be any valid XSD pattern entry), I am trying to work out the maximum possible length the contents of the field in the XML can be.

在这个简单的例子中,它显然是 8 - 我可以通过检查 {n} 并假设这是长度来计算出来.在其他模式中,我也可以检查 * 或 + 并假设无界 - 但我正在寻找一种更通用的方法来实现这一点.

In this trivial example, it's obviously 8 - I could figure that out by checking for {n} and assuming that's the length. In other patterns I could also check for * or + and assume unbounded - but I am looking for a more general approach that can be used to do this.

请注意,不保证模式字符串与 .Net 的 Regex 类兼容.

Note that the pattern string is not guaranteed to be compatible with .Net's Regex classes.

我还可以访问 XmlSchemaPatternFacet 类,如果有帮助的话,我首先用它来解析 XSD.

I also have access to the XmlSchemaPatternFacet class, which is what I'm using to parse the XSD in the first place, if that's any help.

感谢您提供的任何帮助

推荐答案

您几乎肯定需要解析正则表达式来实现这一点.例如,您可以采用 Saxon 中的开源正则表达式解析器(它实现了正确的正则表达式方言).这将创建一个子表达式树,您可以添加一个方法来计算树中每个节点的最大匹配长度.Operation 类,代表树中的一个节点,已经(在 9.6 中)有方法 getMatchLength()getMinimumMatchLength(),它会很容易添加一个以相同方式工作的 getMaximumMatchLength().

You'll almost certainly need to parse the regular expression to achieve this. For example you could take the open-source regular expression parser in Saxon (which implements the right regex dialect). This creates a tree of subexpressions, and you could add a method to compute the maximum match length for each node in the tree. The Operation class, representing a node in the tree, already (in 9.6) has methods getMatchLength() and getMinimumMatchLength(), and it would be easy enough to add a getMaximumMatchLength() that works the same way.

这篇关于在 xsd (C#) 中计算与正则表达式匹配的字符串的最大可能长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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