gpt4 book ai didi

java - 想要检查大括号是否存在。但返回假

转载 作者:行者123 更新时间:2023-11-29 03:29:46 25 4
gpt4 key购买 nike

我试图检查 String 的花括号是否存在。但它返回 false。谁能给我一个解决方案。

String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");}}";
Pattern p = Pattern.compile("^[{]");
Matcher m = p.matcher(code);
System.out.println(m.matches());

提前致谢

最佳答案

需要评论

// the first character must be a {
Pattern p = Pattern.compile("^[{]");
Matcher m = p.matcher(code);
// the entire strings must match so you only accept "{" and nothing else.
System.out.println(m.matches());

我怀疑你不想要 ^ 而你想要 find() 而不是 matches() Find 将接受第一个子字符串匹配。

关于java - 想要检查大括号是否存在。但返回假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com