3

これらの単語を見つける方法がわかりません..例私はこのテキストを持っています...

The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> .

何を検索すればいいのかわからず、Googleで検索する<>無視されます。この文字列を取得する方法を教えてください。

だから私は、、、、<location><plural-noun>取得<location><adjective>ます<location>

charAt()メソッドを使用する必要があります。私の試み:

String string = this.fileName;
for(int i = 0; i < string.length(); i++)
                if((string.charAt(i) == '<') && (string.charAt(i) == '>'))
                    System.println(""); //<-------- IM STUCK HERE

わかりません...ほぼ2日間寝ていません。

私の現在の最後の問題です...表示されている各単語を削除する<にはどうすればよいですか?>

String string = this.template;
        Pattern pattern = Pattern.compile("<.*?>");
        Matcher matcher = pattern.matcher(string);

        List<String> listMatches = new ArrayList<String>();

        while(matcher.find()) {
            listMatches.add(matcher.group());
        }
        // System.out.println(listMatches.size());
        int indexNumber = 1;
         for(String s : listMatches) {
             System.out.println(Integer.toString(indexNumber) + ". " + s);
             indexNumber++;
         }
4

3 に答える 3

5

PatternおよびMatcherクラスを使用できます。

  1. regex Pattern を検索し<.*?>ます。
  2. Matcher でパターンを見つけます。
于 2015-10-04T06:05:51.993 に答える
1

行全体を読み取って、たとえば に保存しますString line。次に、次を使用します。

String line = "The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> ."; 

boolean found = false;
String data[] = new String[20];
int counter = 0;

Arrays.fill(data, "");

for(int i = 0; i < line.length() && counter < 20; i++) {
    if(line.charAt(i) == '<')
        found = true;
    else if(line.charAt(i) == '>' && found) {
        found = false;
        counter++;
    }
    else if(found) {
        data[counter] += line.charAt(i);
    }
}

for(int i = 0; i < counter; i++)
    System.out.println("Scanned data #" + (i + 1) + " = " + data[i]);
于 2015-10-04T06:23:43.647 に答える