java - 将字符串中的所有大括号导出到数组
问题描述
例子:
String = "(a(b[}7cv)))"
数组应如下所示:["(","(","[","}",")",")",")"]
我的尝试:
if(s.charAt(i) == "(" ){
//add to array}
解决方案
您可以简单地使用正则表达式找到它们并将它们添加到列表中
public static void main(String[] args) {
String foo = "(a(b[}7cv)))";
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("\\W")
.matcher(foo);
while (m.find()) {
allMatches.add(m.group());
}
System.out.println(allMatches);
}
如果它只是您所追求的括号,您可能希望将"[(){}\\]\\[]"
其作为正则表达式Pattern.compile
推荐阅读
- vue.js - 为什么 SCSS 类在推送到生产 (AWS) 时未在 Nuxt 中应用/编译?
- bash - 如何在case语句中接受与多个变量匹配的多个输入?
- c++ - C++ 中的去抖动
- html - 水平 ul 列表中的神秘左侧填充
- docker - 使用 Docker chown 容器目录 (ubuntu - mysql/mysql-server)
- python - TypeError:“响应”对象不可下标
- html - 我想使用 CSS 更改类的属性值
- windows - 如何下载和安装 Boost
- python - 如何检测多个屏幕
- cypress - 赛普拉斯如何在页面加载结果与所选内容相同后断言