Regex to get first two words of unknown length from a string

If you have only spaces between words, split by \\s+. When you split, the array would be the words themselves. First two would be in arr[0] and arr[1] if you do:

String[] arr = origString.split("\\s+");

With regular expressions you can do something like this:

    public static ArrayList<String> split2(String line, int n){
    line+=" ";
    Pattern pattern = Pattern.compile("\\w*\\s");
    Matcher matcher = pattern.matcher(line);
    ArrayList<String> list = new ArrayList<String>();
    int i = 0;
    while (matcher.find()){
        if(i!=n)
            list.add(matcher.group());
        else
            break;
        i++;
    }
    return list;
}

if you want the first n words, or simply this:

    public static String split3(String line){
    line+=" ";
    Pattern pattern = Pattern.compile("\\w*\\s\\w*\\s");
    Matcher matcher = pattern.matcher(line);
    matcher.find();
    return matcher.group();
}

if you want only the first and second words.


If you want to split it on exactly the space character:

String[] parts = args[i].split(" ");

If you want to split it on any whitespace character (space, tab, newline, cr):

String[] parts = args[i].split("\\s");

To treat multiple adjacent spaces as one separator:

String[] parts = args[i].split(" +");

Same for whitespace:

String[] parts = args[i].split("\\s+");

The first two words would be parts[0] and parts[1]

Tags:

Java

Regex