这是我的文本的格式:
15no16no17yes the parents who have older children always tell you the next stage is worse.18yes using only their hands and feet make some of the worst movies in the history of the world.19no
所以基本格式是这样的:
number
yes|no
text(may/may not be there)
重复
yes
or之后的文本no
可以为空,也可以以空格开头。(我试图在上面说明这一点)。
我的代码适用于这种格式:
number
yes|no
重复
更多要解析的文本示例:
30no31yesapproximately 278 billion miles from anything.32no33no34no
30no31yesapproximately 278 billion miles from anything32no33yessince the invention of call waiting34yesGravity is a contributing factor in 73 percent of all accidents involving falling objects.
35yesanybody who owns hideous clothing36yes if you take it from another person's plate37yes172 miles per hour upside down38yesonly more intelligent39yes any product including floor wax that has fat in it
35no36yestake it from another person's plate37yes172 miles per hour upside down38no39no
35no36no37yes172 miles per hour38no39no
35no36no37yesupside down38no39no
如何修改我的代码?
String regex = "^(\\d+)(yes|no)";
Pattern p = Pattern.compile(regex);
while(input.hasNextLine()) {
String line = input.nextLine();
String myStr = line;
Matcher m = p.matcher(myStr);
while(m.find()) {
String all = m.group();
String digits = m.group(1);
String bool = m.group(2);
// do stuff
myStr = myStr.substring(all.length());
m.reset(myStr);
} // end while
} // end while
我尝试使用String regex = "^(\\d+)(yes|no)(.*)";
,但问题是它捕获了 ayes
或no
.
我该怎么办?
PS:如果有什么不清楚的地方请告诉我,我会提供更多解释。