import java.util.*;
import java.lang.*;

class Main {
	public static void main (String[] args) throws java.lang.Exception {
    	String str = "hello, this is a long sentence";
		System.out.printf("Replaced: [%s]%n", truncateAfteWords(3, str));		
	}

    private static String truncateAfteWords(int n, String str) {
		return str.replaceAll("^((?:\\W*\\w+){" + n + "}).*$", "$1");	
	}  
}