文字列内の文字列(この場合はffg)を見つけ出す必要があります。シーケンスが見つかると前後の文字を返します。文字列内の文字列を見つける
私はmatch.find()を使用してこの方法を見つけることができますが、位置を返し、そこに格納されている文字が必要です。
16 ffg 20
34 ffg 38
47 ffg 51
60 ffg 64
と私は必要があります:
この出力は元に戻りオフセットを
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testes {
public static void main(String[]args){
String text = "aaddbsanklfafjdkgffgkakfjkldsjlkfjdffgkaskdlfkdkffgasjdaeflkaffgaff";
String wordToFind = "ffg";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(text);
while (match.find()) {
System.out.print(match.start() - 1);
System.out.print(" " + wordToFind + " ");
System.out.println(match.end());
}
}
}
'String.charAt(match.start() - 1)'? –
投稿したコードの問題点は何ですか? – tnw