私の基本的な問題は次のとおりです。ファイルからいくつかの行を読み込んでいます。ファイルの各行の形式はこれです:文字列内の2つの単語を検索するための正規表現
John Doe 123
がDoe
と123
間のタブがあります。
John Doe
を「取り除く」ような正規表現を探しています。 scanner.next(regular expression)
のようなものは、私にJohn Doe
を与えるでしょう。
これはおそらく非常に簡単ですが、動作させることはできません。また、私はそこにあるタブに頼ることなくこれを理解しようとしています。
私はここで見ました:Regular Expression regex to validate input: Two words with a space between。しかし、これらの答えのどれも働かなかった。私はランタイムエラーが発生し続けた。
いくつかのコード:
while(inFile.hasNextLine()){
String s = inFile.nextLine();
Scanner string = new Scanner(s);
System.out.println(s); // check to make sure I got the string
System.out.println(string.next("[A-Za-z]+ [A-Za-z]+")); //This
//doesn't work for me
System.out.println(string.next("\\b[A-Za-z ]+\\b"));//Nor does
//this
}
私たちが作業できるコードはありますか? –
(John)。+(Doe) - http://docs.oracle.com/javase/tutorial/essential/regex/index.html –
「[区切りを使用して区切り文字「\ t」でJavaでの文字列解析]の可能な複製(http://stackoverflow.com/questions/1635764/string-parsing-in-java-with-delimeter-tab-t-using-split) –