有没有一种理论框架EX pression大小限制"或"运营商对Regex.Replace [英] Is there a theorical expression size limit for "or" operator on Regex.Replace

查看:122
本文介绍了有没有一种理论框架EX pression大小限制"或"运营商对Regex.Replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个理论框架EX pression大小限制在Regex.Replace或操作符 如Regex.Replace(ABC,(A | C | D | E ...继续说500000元素在这里),ZZZ)

Is there a theorical expression size limit for "or" operator on Regex.Replace such as Regex.Replace("abc","(a|c|d|e...continue say 500000 elements here)","zzz") ?

在.NET的实现任何stackoverflowException?

Any stackoverflowException on .NET's implementation ?

感谢

推荐答案

没有理论上限,但每一个普通的前pression引擎都会有自己的实现的限制。在这种情况下,由于您使用的是.NET的限制是由于.NET运行时可以使用的内存量。

There is no theoretical limit, though each regular expression engine will have its own implementation limits. In this case, since you are using .NET the limit is due to the amount of memory the .NET runtime can use.

一个普通EX pression一百万alernations正常工作对我来说:

A regular expression with one million alernations works fine for me:

string input = "a<142>c";
var options = Enumerable.Range(0, 1000000).Select(x => "<" + x + ">");
string pattern = string.Join("|", options);
string result = Regex.Replace(input, pattern, "zzz");

结果:

azzzc

这是非常缓慢的,但。越来越多的期权数量10万给我的 OutOfMemoryException异常

您可能会受益于寻找另一种方法。

You probably would benefit from looking at another approach.

这篇关于有没有一种理论框架EX pression大小限制&QUOT;或&QUOT;运营商对Regex.Replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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