String (String s) { StringBuilder sb = new StringBuilder(); boolean escaped = false; instantApplied = 0; for (char c : s.toCharArray()) { if (c == '*') { if (escaped) { sb.append("\\*"); escaped = false; } else { sb.append(".*"); } } else if (c == '?') { if (escaped) { sb.append("\\?"); escaped = false; } else { sb.append("."); } } else if (c == '\\') { if (escaped) { sb.append("\\\\\\\\"); } escaped = !escaped; } else { if (escaped) { sb.append("\\\\"); escaped = false; } switch (c) { case '[', ']', '^', '$', '+', '{', '}', '|', '(', ')', '.' -> sb.append('\\').append(c); default -> sb.append(c); } } } if (escaped) { sb.append("\\\\"); } if (instantMode) { int l = sb.length(); boolean okStart, okEnd; if (l < 2) { okStart = okEnd = false; } else { okStart = sb.substring(0, 2).equals(".*"); okEnd = sb.substring(l - 2).equals(".*"); } if (!okStart) { instantApplied = 1; sb.insert(0, ".*"); } if (!okEnd) { instantApplied += 2; sb.append(".*"); } } return sb.toString(); }