решений без использования шаблонов:
str.startsWith("preference_network_");
str.contains("preference_network_");
С шаблонами
// the same as contains.
Pattern p = Pattern.compile("preference_network_");
p.matcher(str).find();
// the same as startsWith.
Pattern p = Pattern.compile("^preference_network_");
p.matcher(str).find();
Если вы хотите использовать matches()
, вам нужно написать полный шаблон:
Pattern p = Pattern.compile("^preference_network_.*");
p.matcher(str).matches();
Поскольку совпадения выполняются так, как если бы ваш шаблон начинался с ^ и заканчивался $, то есть
Pattern.compile("^something$").matcher(str).find()
совпадает с Pattern.compile("something").matcher(str).matches()