これは私のテキストの形式です:
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
またはの後のテキスト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)(.*)";
の後のすべてをキャプチャすることです。yes
no
私は何をしますか?
PS: 不明な点があればお知らせください。さらに説明します。