import re while True: try: list1 = input().split() rule = input() res = list() for p in rule: if p == "*": res.extend(["(", ".", "*", ")"]) continue res.append(p) rule = "".join(res) ans = [str(i) for i in range(len(list1)) if re.match(rule, list1[i])] print(",".join(ans) if ans else 0) except: break ,上述代码用java输出
时间: 2024-02-14 16:29:44 浏览: 125
react-list-input:您的用户需要管理物品清单吗? 编辑吗? 拖拉它吗? 用这个!
以下是将上述代码转换为 Java 的版本:
```java
import java.util.Scannerimport java.util.regex.Matcher;
java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
try {
String[] list1 = scanner.nextLine().split(" ");
String rule = scanner.nextLine();
StringBuilder res = new StringBuilder();
for (char p : rule.toCharArray()) {
if (p == '*') {
res.append("(.*)");
} else {
res.append(p);
}
}
String regex = res.toString();
Pattern pattern = Pattern.compile(regex);
StringBuilder ans = new StringBuilder();
for (int i = 0; i < list1.length; i++) {
Matcher matcher = pattern.matcher(list1[i]);
if (matcher.matches()) {
ans.append(i).append(",");
}
}
if (ans.length() > 0) {
System.out.println(ans.substring(0, ans.length() - 1));
} else {
System.out.println("0");
}
} catch (Exception e) {
break;
}
}
}
}
```
请注意,此代码是将原始代码直接转换为 Java 的版本,并可能需要根据实际情况进行调整和优化。
阅读全文