解决方案 »

  1.   

    不知道这个地方是否应该用while
      

  2.   

    如果在while的最末尾加上break;一切都ok,死循环到底在哪里?
      

  3.   


    ....
    for(String secondElement:new String[]{"\\were\\w","\\were","T\\w+","Never.*?!"}){
                  examine(element,secondElement);
     }
    .....
    input: As long as there is injustice,whenever a
    \were\w
    \were
    find() 'here' start=12,end=16
    T\w+
    Never.*?!
    input: baby cires out, wherever a distress
    \were\w
    find() 'herev' start=17,end=22
    \were
    find() 'here' start=17,end=21
    T\w+
    Never.*?!
    input: signal sounds among the starts ... We all be there.
    \were\w
    \were
    find() 'here' start=46,end=50
    T\w+
    Never.*?!
    input: This fine ship, and this fine crew ...Never give up, Never surrender!
    \were\w
    \were
    T\w+
    find() 'This' start=0,end=4
    lookingAt() 'This' start=0,end=4
    Never.*?!
    find() 'Never give up, Never surrender!' start=38,end=69
      

  4.   

    预定义字符类 
    . 任何字符(与行结束符可能匹配也可能不匹配) 
    \d 数字:[0-9] 
    \D 非数字: [^0-9] 
    \s 空白字符:[ \t\n\x0B\f\r] 
    \S 非空白字符:[^\s] 
    \w 单词字符:[a-zA-Z_0-9] 
    \W 非单词字符:[^\w] 
    Greedy 数量词 
    X? X,一次或一次也没有 
    X* X,零次或多次 
    X+ X,一次或多次 
    X{n} X,恰好 n 次 
    X{n,} X,至少 n 次 
    X{n,m} X,至少 n 次,但是不超过 m 次 
      
    Reluctant 数量词 
    X?? X,一次或一次也没有 
    X*? X,零次或多次 
    X+? X,一次或多次 
    X{n}? X,恰好 n 次 
    X{n,}? X,至少 n 次 
    X{n,m}? X,至少 n 次,但是不超过 m 次 
      

  5.   

    \w 是词字符,奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?
      

  6.   


    你可以使用点 . 试试.是任意字符,他会把一切都包含进去,怎么破。而你的正则没有表达出我的业务逻辑啊?
    你原先的写法没有问题,问题出在这里:if(m.lookingAt()){
                    System.out.println("lookingAt() '"+m.group()+"' start="+m.start()+",end="+m.end());
                }
                if(m.matches()){
                    System.out.println("matches() '"+m.matches()+"' start="+m.start()+",end="+m.matches());
                }你把它注释掉看看
      

  7.   


    你可以使用点 . 试试.是任意字符,他会把一切都包含进去,怎么破。而你的正则没有表达出我的业务逻辑啊?
    你原先的写法没有问题,问题出在这里:if(m.lookingAt()){
                    System.out.println("lookingAt() '"+m.group()+"' start="+m.start()+",end="+m.end());
                }
                if(m.matches()){
                    System.out.println("matches() '"+m.matches()+"' start="+m.start()+",end="+m.matches());
                }你把它注释掉看看lookingAt()   尝试将从区域开头开始的输入序列与该模式匹配
      

  8.   

    比如字符串实例:“As long as there is injustice,whenever a”
    正则表达式:"\\w*ere\\w*"
    执行:
    while(m.find()){
               //打印m.group();
                if(m.lookingAt()){
                     //打印m.group();
                }
                if(m.matches()){
                     //打印m.group();
                }
    }
    执行过程是:“while(m.find())”→吞没方式匹配到“there”→“ if(m.lookingAt())”→从新回到字符串实例的起始字符开始匹配,即匹配“As ...”→匹配不成功→if(m.matches())→整个字符串实例进行匹配,其实也是从字符串实例的首字母开始匹配,就当前的字符串实例来说,永远匹配不成功,但是它会吞没到“As ”,剩下的字符串实例是“long as there is injustice,whenever a“→“while(m.find())”→吞没方式匹配到“there”→上面的流程死循环...
    上面就是造成死循环的原因
      

  9.   

    解释相当的合理,就是重置查找位置导致的,在这里书上写到no reset()nessary,难道thinking in java错误,分给你!
      

  10.   

    问题在于你在循环中用了 lookingAt  matches 两个函数,这将使得mather回到开头,导致 find不停的找到同一处。将 lookingAt 和 matches 去掉就可以了。