解释下面的代码并填空 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-03-31 22:05:01 浏览: 88
&& 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",表示只有当规则字符串和待匹配字符串都还有字符未匹配时,才进行匹配。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)