3

I have this code which prints:

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

I tried to split at [#] but it didnt work.

What should i put in split so that I can get as a result the part after # only: Hello, 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);

3 Answers 3

7

If I understand correctly, you only want to extract "Hello" and "Bye" from your input String through regex.

In which case, I would just use iterative matching of whatever's in between # and >, as such:

// 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));
}

Output:

Hello
Bye
Sign up to request clarification or add additional context in comments.

9 Comments

+1, beat me to it, but use "#([^>]+)>", it can be much faster than using reluctant quantifiers.
@Brian thanks for the improvement tip and fair play! Will edit my answer now.
I want the output to be: Hello, Bye and if i use this I cant get results, because the string input = "..." you wrote is not always the same and changes depending on user input. (example type 1,2 and get different results from 3,4). However I always need to get the same part between # and >
@user2598911 sorry went to the gym. Yes, with yourArrayListInstance.add(matcher.group(1)) in this instance.
You're probably printing the ArrayList instead of each of its elements - I'd advise to take a look at the javadoc. Using m.group() will add the whole match; as said you want m.group(1) which will add what's between # and >.
|
2

You can try this

String[] str= your_orginal_String.split(",");

Then you can take the parts after # as follows

String[] s=new String[2];
int j=0;
for(String i:str){
    s[j]=i.split("#",2)[1];
    j++;
}

You may need some formatting. for resulting String[] s as follows

    String str = "[( ?Random = <http://www.semanticweb.org/vassilis
                   /ontologies/2013/5/Test#Hello> ), ( ?Random = 
            <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]";
    String[] arr = str.split(",");
    String[] subArr = new String[arr.length];
    int j = 0;
    for (String i : arr) {
        subArr[j] = i.split("#", 2)[1].replaceAll("\\>|\\)|\\]", "");
        j++;
    }
    System.out.println(Arrays.toString(subArr));

Out put:

  [Hello , Bye ]

Comments

0

Try the regular expression:

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

e.g.:

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());
    }
}

Output:

Hello
World

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.