为什么这个正则表达式在.NET中失败?

时间:2016-04-06 15:13:59

标签: c# .net regex

看看这个测试方法:

package javaapplication58;

import java.util.Scanner;
import java.util.Vector;

public class JavaApplication58 {


        public static void main(String[] args) {
            Vector <Integer> v=new Vector();
            v=readInVectorOfIntegers();
            System.out.println(largest(v));

           }
        public static Vector  <Integer> readInVectorOfIntegers()
        {Scanner in=new Scanner(System.in);
        Vector<Integer>w=new Vector ();
        while (in.hasNextLine())w.addElement(in.nextInt());
        return w;

        }

        public static int largest(Vector <Integer> v)
        {int largestsofar;
        if(v.size()>0){largestsofar=v.elementAt(0);
        for (int i=0;i<v.size();i++)
            if(largestsofar<v.elementAt(i))
             largestsofar=v.elementAt(i);
        return largestsofar;


        }

        return 0;


        }
    }

正如目前所写,此代码永远不会从[ Test ] public static void TestRegex() { var goodTextsToTest = new List<string> { "http://google.com", "https://google.com/", "ftp://bugger!!!one1", "ftss://shoot", "somelongergibberish://flkjd", "thescheme://green" }; var badTextsToTest = new List<string> { "bad432:4h//orange", "1ftp://1bugger!!!one1", "IAmTheVeryModelOfAModernMajorGeneral", "" }; var regex = new Regex( "^([a-z][a-z0-9+\\.\\-]*)*://", RegexOptions.IgnoreCase ); foreach( var txt in badTextsToTest ) Assert.IsFalse( regex.IsMatch( txt ), "Passed but should have failed: " + txt ); foreach( var txt in goodTextsToTest ) Assert.IsTrue( regex.IsMatch( txt ), "Failed but should have passed: " + txt ); } 返回。代码被卡住的输入是&#34; IAmTheVeryModelOfAModernMajorGeneral&#34;。

当输入为&#34; IAmTheVeryModelOfAModernMajorGeneral&#34;时,为什么这个正则表达式会导致无限循环?吗

奖金问题: 如果您删除&#34;://&#34;此代码 完成执行从正则表达式。即var regex = new Regex( "^([a-z][a-z0-9+\\.\\-]*)*://", RegexOptions.IgnoreCase ); 为什么要解决此问题?

1 个答案:

答案 0 :(得分:5)

由于回溯,您的正则表达式按现在设计将需要87500多步才能完成。查看debbuger here。这就是我们所说的catastrophic backtracking