Untitled
plain_text
18 days ago
1.7 kB
4
Indexable
Never
public static boolean contains(String string, String pattern) { if (string == null || pattern == null) { return false; } return match(string, pattern, 0, 0); } private static boolean match(String string, String pattern, int stringIndex, int patternIndex) { int sLength = string.length(); int pLength = pattern.length(); while (stringIndex < sLength && patternIndex < pLength) { char sChar = string.charAt(stringIndex); char pChar = pattern.charAt(patternIndex); if (pChar == '\\') { // Check for escaped * if (patternIndex + 1 < pLength && pattern.charAt(patternIndex + 1) == '*') { if (match(string, pattern, stringIndex, patternIndex + 2)) { return true; } } // If backslash is followed by a non-escaped character, treat it as a regular character patternIndex++; } else if (pChar == '*') { // Handle the wildcard by trying to match 0 or more characters in the string. for (int i = stringIndex; i <= sLength; i++) { if (match(string, pattern, i, patternIndex + 1)) { return true; } } } else if (sChar != pChar) { return false; } stringIndex++; patternIndex++; } // If we have reached the end of both string and pattern, it's a match. return stringIndex == sLength && patternIndex == pLength; }