代码解释fix this script public class Main { public static boolean isRegularMatching(String rule, String str) { int lRule = rule.length(); int lStr = str.length(); int iRule = 0; int iStr = 0; while (iRule < lRule && iStr < lStr) { switch (_____ (1)_____) { case '*': { iRule += 1; if (iRule >= lRule) { return true; } else { for (int i = iStr; i < lStr; i++) { if (_____ (2)_____) { return true; } } } break; } case '$': { if (_____(3)_____) { return false; } while ((iStr < lStr) && (str.charAt(iStr) >= '0') && (str.charAt(iStr) <= '9')) { iStr += 1; } iRule += 1; break; } default: { if (rule.charAt(iRule) != str.charAt(iStr)) { _____(4)_____; } iRule += 1; iStr += 1; break; } } } if (iRule < lRule && iStr >= lStr) { if (rule.charAt(iRule) == '*') { return true; } } else { return _____(5)_____; } return false; } }
时间: 2023-04-04 11:05:00 浏览: 69
这段代码是一个正则表达式匹配的方法,其中使用了两个字符串参数,分别是规则和待匹配的字符串。在方法中,使用了两个整型变量来记录规则和字符串的长度,以及两个整型变量来记录规则和字符串的当前位置。在循环中,通过比较规则和字符串的当前位置以及规则中的字符来判断是否匹配成功。
相关问题
解释下面的代码并填空 public class Main { public static boolean isRegularMatching(String rule, String str) { int lRule = rule.length(); int lStr = str.length(); int iRule = 0; int iStr = 0; while (iRule < lRule && iStr < lStr) { switch (_____ (1)) { case '': { iRule += 1; if (iRule >= lRule) { return true; } else { for (int i = iStr; i < lStr; i++) { if ( (2)) { return true; } } } break; } case '$': { if ((3)_____) { return false; } while ((iStr < lStr) && (str.charAt(iStr) >= '0') && (str.charAt(iStr) <= '9')) { iStr += 1; } iRule += 1; break; } default: { if (rule.charAt(iRule) != str.charAt(iStr)) { (4); } iRule += 1; iStr += 1; break; } } } if (iRule < lRule && iStr >= lStr) { if (rule.charAt(iRule) == '') { return true; } } else { return (5); } return false; } }
&& iStr < lStr) {
char cRule = rule.charAt(iRule);
char cStr = str.charAt(iStr);
if (cRule == '.') {
iRule++;
iStr++;
} else if (cRule == '*') {
if (iRule == ) {
return false;
}
char preRule = rule.charAt(iRule - 1);
if (preRule == '.') {
iStr++;
} else {
while (iStr < lStr && str.charAt(iStr) == preRule) {
iStr++;
}
}
iRule++;
} else {
if (cRule != cStr) {
return false;
}
iRule++;
iStr++;
}
}
return iRule == lRule && iStr == lStr;
}
}
这段代码是一个正则表达式匹配的实现,其中 isRegularMatching 方法接受两个参数,分别是规则字符串 rule 和待匹配字符串 str。该方法使用 while 循环遍历规则字符串和待匹配字符串,根据规则中的字符进行匹配。其中,'.' 表示匹配任意单个字符,'*' 表示匹配前一个字符的零个或多个。如果匹配成功,则返回 true,否则返回 false。
空格处应填写 "iRule < lRule && iStr < lStr",表示只有当规则字符串和待匹配字符串都还有字符未匹配时,才进行匹配。
用五岁孩子能听懂的话解释下面的代码 public class Main { public static boolean isRegularMatching(String rule, String str) { int lRule = rule.length(); int lStr = str.length(); int iRule = 0; int iStr = 0; while (iRule < lRule && iStr < lStr) { switch (_____ (1)_____) { case '*': { iRule += 1; if (iRule >= lRule) { return true; } else { for (int i = iStr; i < lStr; i++) { if (_____ (2)_____) { return true; } } } break; } case '$': { if (_____(3)_____) { return false; } while ((iStr < lStr) && (str.charAt(iStr) >= '0') && (str.charAt(iStr) <= '9')) { iStr += 1; } iRule += 1; break; } default: { if (rule.charAt(iRule) != str.charAt(iStr)) { _____(4)_____; } iRule += 1; iStr += 1; break; } } } if (iRule < lRule && iStr >= lStr) { if (rule.charAt(iRule) == '*') { return true; } } else { return _____(5)_____; } return false; } }
这段代码是用来判断一个字符串是否符合给定的规则。代码中定义了两个字符串变量 rule 和 str,分别表示规则和待匹配的字符串。代码中使用了一个 while 循环,通过比较规则和字符串的长度以及两个指针的位置来判断是否匹配成功。具体实现细节需要更深入的了解编程知识。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)