3

私は印刷するこのコードを持っています:

[( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), ( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]

[#] で分割しようとしましたが、うまくいきませんでした。

結果として # のみの後の部分を取得できるように、スプリットに何を入れるべきですか: こんにちは、さようなら

Query query = QueryFactory.create(queryString);
                     QueryExecution qe= QueryExecutionFactory.create(query, model);
                    ResultSet resultset = qe.execSelect();
                    ResultSet results = ResultSetFactory.copyResults(resultset); 
                    final ResultSet results2 = ResultSetFactory.copyResults(results);


                    System.out.println( "== Available Options ==" );
                    ResultSetFormatter.out(System.out, results, query);



    Scanner input = new Scanner(System.in);
    final String inputs;
    inputs = input.next();
    final String[] indices = inputs.split("\\s*,\\s*");

    final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>(
            indices.length) {
        {
            final List<QuerySolution> solutions = ResultSetFormatter
                    .toList(results2);
            for (final String index : indices) {
                add(solutions.get(Integer.valueOf(index)));
            }
        }
    };

    System.out.println(selectedSolutions);
4

3 に答える 3

7

私の理解が正しければ、正規表現を使用して入力文字列から「Hello」と「Bye」のみを抽出する必要があります。

その場合、 と の間にあるものの反復マッチングを使用するだけ#です>

// To clarify, this String is just an example
// Use yourScannerInstance.nextLine to get the real data
String input = "[( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), "
                + "( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]";
// Pattern improved by Brian
// was: #(.+?)>
Pattern p = Pattern.compile("#([^>]+)>");
Matcher m = p.matcher(input);
// To clarify, printing the String out is just for testing purpose
// Add "m.group(1)" to a Collection<String> to use it in further code
while (m.find()) {
    System.out.println(m.group(1));
}

出力:

Hello
Bye
于 2013-09-17T16:39:53.977 に答える
0

正規表現を試してください:

(?<=#)([^#>]+)

例えば:

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("(?<=#)([^#>]+)");

public static void main(String[] args) {
    String input = "[( ?A = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), ( ?A = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#World> )]";
    Matcher matcher = REGEX_PATTERN.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

出力:

Hello
World
于 2013-09-17T19:18:56.610 に答える