user2598911
user2598911

Reputation: 379

Regex to split string

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

Upvotes: 3

Views: 606

Answers (3)

Paul Vargas
Paul Vargas

Reputation: 42010

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

Upvotes: 0

Mena
Mena

Reputation: 48404

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

Upvotes: 7

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

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 ]

Upvotes: 2

Related Questions