java - 按 Java 中第一个找到的字符串拆分

标签 java regex jakarta-ee java-6

是否可以告诉 String.split("(") 函数它必须仅按找到的第一个字符串 "("进行拆分?

例子:

String test = "A*B(A+B)+A*(A+B)";
test.split("(") should result to ["A*B" ,"A+B)+A*(A+B)"]
test.split(")") should result to ["A*B(A+B" ,"+A*(A+B)"]

最佳答案

是的,绝对的:

test.split("\\(", 2);

作为 the documentation for String.split(String,int)解释:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

关于java - 按 Java 中第一个找到的字符串拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9873674/

相关文章:

regex -\b 与 Regex/Python 的行为不符预期

java - Tomcat - 请求的资源不可用 - IntelliJ IDEA - Java EE

java - 如何在opencv java中裁剪检测到的人脸图像

java - 有没有办法在 javaFx 应用程序启动时禁用/启用该应用程序中的元素,即 Pane 和按钮

java - 在正则表达式JAVA中使用Pattern Matcher时如何获取剩余的不匹配字符串?

java - 在 Datatable JSF 中显示 Set 集合中的对象不起作用

java.io.FileNotFoundException : Could not open ServletContext resource [/WEB-INF/final-servlet. xml]

java - 如何从 java 中收集 JVM 性能统计信息

java - 中位数中位数的奇怪错误作为找到第 K 个最大元素的枢轴

java - RegEx 在 JAVA 中搜索确切的单词