thinking in java中的一个例子,我稍微改了一下,就死循环了,指导下!

bh1990 2014-06-09 08:54:20
package Chapter13_String;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StartEnd {
public static String str=
"As long as there is injustice,whenever a\n"+
"baby cires out, wherever a distress\n"+
"signal sounds among the starts ... We all be there.\n"+
"This fine ship, and this fine crew ..."+
"Never give up, Never surrender!";
public static void examine(String s,String regex){
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(s);
System.out.println(regex);
while(m.find()){
//当在匹配中查找到相应的匹配时==一次查找,可以进行多次匹配操作
System.out.println("find() '"+m.group()+"' start="+m.start()+",end="+m.end());
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());
}
}
}
public static void main(String[] args){
for(String element:str.split("\n")){
System.out.println("input: "+element);
for(String secondElement:new String[]{"\\w*ere\\w*","\\w*ere","T\\w+","Never.*?!"}){
examine(element,secondElement);
}
}
}
}

死循环
input: As long as there is injustice,whenever a
find() 'there' start=11,end=16
find() 'there' start=11,end=16
find() 'there' start=11,end=16
find() 'there' start=11,end=16
find() 'there' start=11,end=16
......
...全文
440 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
风_流沙 2014-06-10
  • 打赏
  • 举报
回复
学习了,哈哈哈
twtiqfn 2014-06-10
  • 打赏
  • 举报
回复
上学时学了一段时间,感觉好难啊
yanyanem 2014-06-09
  • 打赏
  • 举报
回复
问题在于你在循环中用了 lookingAt matches 两个函数,这将使得mather回到开头,导致 find不停的找到同一处。 将 lookingAt 和 matches 去掉就可以了。
bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 15 楼 disalong 的回复:
比如字符串实例:“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”→上面的流程死循环... 上面就是造成死循环的原因
解释相当的合理,就是重置查找位置导致的,在这里书上写到no reset()nessary,难道thinking in java错误,分给你!
「已注销」 2014-06-09
  • 打赏
  • 举报
回复
比如字符串实例:“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”→上面的流程死循环... 上面就是造成死循环的原因
同同 2014-06-09
  • 打赏
  • 举报
回复
引用 13 楼 lytlx 的回复:
[quote=引用 12 楼 bh1990 的回复:] [quote=引用 11 楼 lytlx 的回复:] [quote=引用 10 楼 bh1990 的回复:] [quote=引用 9 楼 lytlx 的回复:] 预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
\w 是词字符,
引用 8 楼 lytlx 的回复:

....
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

奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?[/quote] 你可以使用点 . 试试[/quote].是任意字符,他会把一切都包含进去,怎么破。而你的正则没有表达出我的业务逻辑啊?[/quote] 你原先的写法没有问题,问题出在这里:

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());
            }
你把它注释掉看看[/quote] lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配
同同 2014-06-09
  • 打赏
  • 举报
回复
引用 12 楼 bh1990 的回复:
[quote=引用 11 楼 lytlx 的回复:] [quote=引用 10 楼 bh1990 的回复:] [quote=引用 9 楼 lytlx 的回复:] 预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
\w 是词字符,
引用 8 楼 lytlx 的回复:

....
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

奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?[/quote] 你可以使用点 . 试试[/quote].是任意字符,他会把一切都包含进去,怎么破。而你的正则没有表达出我的业务逻辑啊?[/quote] 你原先的写法没有问题,问题出在这里:

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());
            }
你把它注释掉看看
bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 11 楼 lytlx 的回复:
[quote=引用 10 楼 bh1990 的回复:] [quote=引用 9 楼 lytlx 的回复:] 预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
\w 是词字符,
引用 8 楼 lytlx 的回复:

....
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

奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?[/quote] 你可以使用点 . 试试[/quote].是任意字符,他会把一切都包含进去,怎么破。而你的正则没有表达出我的业务逻辑啊?
同同 2014-06-09
  • 打赏
  • 举报
回复
引用 10 楼 bh1990 的回复:
[quote=引用 9 楼 lytlx 的回复:] 预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
\w 是词字符,
引用 8 楼 lytlx 的回复:

....
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

奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?[/quote] 你可以使用点 . 试试
bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 9 楼 lytlx 的回复:
预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
\w 是词字符,
引用 8 楼 lytlx 的回复:

....
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

奇怪,为什么改个正则,就不死循环了,,,\\w*ere\\w*我这个意思是“词字符(0-多个)+ere+词字符(0-多个)”,虽然你对了,但是我这种业务逻辑怎么操作呢?
同同 2014-06-09
  • 打赏
  • 举报
回复
预定义字符类 . 任何字符(与行结束符可能匹配也可能不匹配) \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 次
同同 2014-06-09
  • 打赏
  • 举报
回复

....
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

bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 1 楼 coolbamboo2008 的回复:
不知道这个地方是否应该用while
如果在while的最末尾加上break;一切都ok,死循环到底在哪里?
bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 5 楼 bh1990 的回复:
[quote=引用 4 楼 lytlx 的回复:] [quote=引用 3 楼 lytlx 的回复:] [quote=引用 2 楼 lytlx 的回复:] 问题出在你的\\w上面
因为\\w可以是任何单词字符[/quote] 把你的* 去掉看看效果是不是就没有死循环了[/quote]不对啊[/quote]说的完全对不上头,
bh1990 2014-06-09
  • 打赏
  • 举报
回复
引用 4 楼 lytlx 的回复:
[quote=引用 3 楼 lytlx 的回复:] [quote=引用 2 楼 lytlx 的回复:] 问题出在你的\\w上面
因为\\w可以是任何单词字符[/quote] 把你的* 去掉看看效果是不是就没有死循环了[/quote]不对啊
同同 2014-06-09
  • 打赏
  • 举报
回复
引用 3 楼 lytlx 的回复:
[quote=引用 2 楼 lytlx 的回复:] 问题出在你的\\w上面
因为\\w可以是任何单词字符[/quote] 把你的* 去掉看看效果是不是就没有死循环了
同同 2014-06-09
  • 打赏
  • 举报
回复
引用 2 楼 lytlx 的回复:
问题出在你的\\w上面
因为\\w可以是任何单词字符
同同 2014-06-09
  • 打赏
  • 举报
回复
问题出在你的\\w上面
coolbamboo2008 2014-06-09
  • 打赏
  • 举报
回复
不知道这个地方是否应该用while

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧