Here's a naive solution:
String input = "saya sedang makan nasi goreng";
Matcher m = Pattern.compile("[^aeiou]?[AEIOUaeiou](ng|n)?").matcher(input);
int s = 0;
while (m.find()) {
System.out.println(input.substring(s, m.end()).trim());
s = m.end();
}
Edit:
@Stephen C is right. Here's a proper solution based on syllable formation rules of the Indonesian language (from source)
In Indonesian a syllable consists of a vowel plus the immediately
precending consonant. It also includes anyh following consonant that
does not immediately precede the next vowel.
Note that ng counts as a single consonant.
String input = "SAYA sedang makan nasi goreng garam asal saat air ia bentuk";
Matcher m = Pattern.compile("[^aeiou]?[aeiou]((ng|[^aeiou])(?![aeiou]))?",
Pattern.CASE_INSENSITIVE).matcher(input);
int s = 0;
while (m.find()) {
System.out.println(input.substring(s, m.end()).trim());
s = m.end();
}
Please note that (also mentioned in the source above) syllables as they are pronounced in speech may be slightly different, e.g. in-speech: ma-kan-an, program output: ma-ka-nan.
Edit 2:
OK. Further studying revealed that I have missed out the ny, sy and kh consonants. Also fixed couple of other problems. Here's the updated regular expression:
"(ng|ny|sy|kh|[^aeiou])?[aeiou]((ng|ny|sy|kh|([^aeiou](?![gyh]))(?![aeiou])))?"