English 中文(简体)
java[复制]的处决速度太慢。
原标题:Regex execution is too slow in java [duplicate]
  • 时间:2010-12-21 14:51:19
  •  标签:
  • java
  • regex

My purpose is to match this kind of different urls:
url.com
my.url.com
my.extended.url.com
a.super.extended.url.com
and so on...

因此,我决定建立监管机构,以便有一封信件或数件,并且有一定数量的“子”,具有字母数字特性和标记。 例如,在“婚姻”中,“婚姻”中的“m”是指从“com”中排出的第一类,“m”是该reg的最后一个类别,而“y”、“the”、“extile”和“url”是该reg的第二类。

采用以下守则的模式和主题,我希望找到办法,把我遣返,因为这一ur子不能相配,而是使用万国邮联的100%,而且似乎停留在无限的住所。

    
    String subject = "www.association-belgo-palestinienne-be";
    Pattern pattern = Pattern.compile("^[A-Za-z0-9]\.?([A-Za-z0-9_-]+\.?)*[A-Za-z0-9]\.[A-Za-z]{2,6}");

    Matcher m = pattern.matcher(subject);
    System.out.println("    Start");
    boolean hasFind = m.find();
    System.out.println("    Finish : " + hasFind);
  

仅印刷:

  
      Start
  

I can t reproduce the problem using regex testers.
Is it normal ? Is the problem coming from my regex ?
Could it be due to my Java version (1.6.0_22-b04 / JVM 64 bit 17.1-b03) ?

事先获得帮助。

最佳回答

它不是无限的 lo。 问题是,它检查了所有可能的匹配,而不是找到。 如果你允许它花了数十年的时间,它将最终终止。 见http://swtch.com/~rsc/regexp/regexp1.html。 第条 很好地解释了在世系发生的事情。

或许这种经常表述令人满意(在规定的座右上终止):^[A-Za-z0-9][A-Za-z0-9_-]*([A-Za-z0-9_-]+*/[A-Za-z]{2,6}$(见http://ideone.com/Z0rlg)

问题回答

问题是:<代码>([A-Za-z0-9_-]+/?]部分定期表述。 请注意,在另一个标的(*)中,它有一台核装置(+)。 http://www.ual-expressions.info/catastrophic.html - 基本上,它必须尝试一定数量的配对,以便检查经常表达,至少是最正常的表述发动机(包括 Java)。

If you use possessive quantifiers, you will be able to avoid this problem, however that would change the meaning of your regex, and it would no longer match what you want it to match.

我认为,这里的陷阱是找到一种可以表达你想要解决的reg,而不会有双重qu。 例如,以下工作应当:

Pattern.compile("^[A-Za-z0-9]\.?([A-Za-z0-9_-]|[A-Za-z0-9_-]\.)*[A-Za-z0-9]\.[A-Za-z]{2,6}$");

我认为,这体现了你试图匹配的那种扼杀,应当更快。

这确实是一个无限的循环,它只是花了很长的时间。 为了所有实际目的,我们可以把它称作hang。

你的雷切尔可能有所改善。

要求到底再提美元。 它将说,这是这条线的结束。 这可能有助于你节省时间。

http://www.ohchr.org。

 String subject = "www-association-belgo-palestinienne-be";
 Pattern pattern = Pattern.compile("^[A-Za-z0-9]([-_A-Za-z0-9]*)(\.[-_A-Za-z0-9]+)*\.([-_A-Za-z0-9]+\.)*([-_A-Za-z0-9]*)[A-Za-z0-9]$");

 Matcher m = pattern.matcher(subject);
 System.out.println("    Start");
 boolean hasFind = m.find();
 System.out.println("    Finish : " + hasFind);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签