Ramses
Ramses

Reputation: 672

How can I split certain phrase a String?

I have String with some search items and I want to split them in an array of String.

Example:

String text = "java example \"this is a test\" hello world";

I want to get the following results

result[0] = "java";
result[1] = "example";
result[2] = "\"this is a test\"";
result[3] = "hello";
result[4] = "world";

In short, I want to combine text.split(" ") and text.split("\""); Is there an easy way to code it?

Thanks!

Upvotes: 1

Views: 58

Answers (3)

adamretter
adamretter

Reputation: 3517

I think you are a bit confused and there are errors in your code! Composing your string should be:

String text = "java example \"this is a test\" hello world";

The value of the variable text would then be:

java example "this is a test" hello world

I am rather assuming that you want to extract this into the following array:

result[0] = "java";
result[1] = "example";
result[2] = "\"this is a test\"";
result[3] = "hello";
result[4] = "world";

You can do this by using a regular expression, for example:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Example {

    public static void main(String[] args) {

        String data = "java example \"this is a test\" hello world";

        Pattern p = Pattern.compile("((?:\"[a-z\\s]+\")|[a-z]+)");
        Matcher m = p.matcher(data);

        List<String> lst = new ArrayList<String>();
        while(m.find()) {
            lst.add(m.group(1));
        }

        String[] result= new String[lst.size()];
        result = lst.toArray(results);

        for(String s: result) {
            System.out.println(s);
        }
    }
}

The regular expression ((?:\"[a-z\\s]+\")|[a-z]+) will match on either: 1) sequences of characters a to z or whitespace between double quotes 2) sequence of characters a to z.

We then extract these matches using m.find

Upvotes: 1

anubhava
anubhava

Reputation: 785186

You can use this regex in String#split method:

(?=(([^\"]*\"){2})*[^\"]*$)\\s+

Code:

String text = "java example \"this is a test\" hello world";
String[] tok = text.split("(?=(([^\"]*\"){2})*[^\"]*$)\\s+");
// print the array
System.out.println( Arrays.toString( arr ) );

Output:

[java, example, "this is a test", hello, world]

Upvotes: 2

Jack
Jack

Reputation: 5768

This regex should match (\\".+?\\")|([^\s]+)

It matches anything within \" including the \" OR single words.

Check here for results: http://www.regexr.com/399a4

Upvotes: 1

Related Questions