How would I remove all square brackets ("[]") from a given String in Java?
String s = "[abcdefg]";
s = s.replaceAll(regex, "");
What regular expression would be used in this case?
Use this one:
String s = "[abcdefg]";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s);
你可以用"\\\\[([^\\\\]])\\\\]"
类的东西来匹配它(打开brachet,一系列任何不是结束括号的东西(包含在里面()
供以后参考),然后关闭括号)然后用()
块(组1)内匹配的内容替换整个匹配(组0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.