public String replace(String s) { Matcher m = Pattern.compile("\\{PLAYER\\-[0-9]*\\-NAME\\}").matcher(s); while(m.find()) { String raw = m.group(); String match = raw.substring(8); match = match.subSequence(0, match.indexOf("-")).toString(); System.out.println(match); if(isInt(match)) { int index = Integer.valueOf(match)-1; if(list.size() > index) {//lista z nazwami posortowanymi s = s.replaceFirst(Pattern.quote(raw), list.get(Integer.valueOf(match)-1));//lista z nazwami posortowanymi continue; } } s = s.replaceFirst(Pattern.quote(raw), "BRAK"); } return s; } public boolean isInt(String s){ try { Integer.valueOf(s); return true; } catch(Exception ex) {} return false; }