English 中文(简体)
为什么这种僵局缓慢? 能否加快?
原标题:Why is this regex slow? Can it be faster?
  • 时间:2012-01-12 20:00:33
  •  标签:
  • .net
  • regex
re = new Regex ((.*?)someliteraltext(.*?moreliteral), RegexOptions.Singleline);
re.Match(c);

Note that Singleline is used, so that "." matches newline.

我把这 run在大约100k个字体上,几分钟。

Can it be faster?

最佳回答

我同意以下意见,即最有可能减慢速度的一点是开始(*?)。 如果在第一个“部分内容”之前有1,000个特性,该特性已经达到1001个,相当于该部分。 @CodeInChaos关于预设^的建议(引自扼)是限制这些匹配的快速途径。 如果这真是可以接受的,你就不得不更多地解释你为获得更好的答案而重新尝试做些什么。

问题回答

It s slow because it requires a lot of backtracking. The article here:

rel=“nofollow”http://www. mal-expressions.info/engine.html

might give you some idea of just how much work it s doing.

As @Wrikken suggested, by removing the initial "(.*?)". This capture group will capture everything from the start of the string until "someliteraltext".

或者,使用“IndexOf”来寻找“某些内容”,然后在“更进一步”之后使用。 这应当更快。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签