java - ^ 正则表达式分割什么样的字符串

标签 java regex

如果我有类似的 split 语句

String array1[]= test.split("^");

可能的测试字符串值是什么,这样这个(“^”)正则表达式将分割测试字符串?

我知道 ^ 是 Java 正则表达式中的特殊字符 - 它意味着“匹配输入的开头”。

最佳答案

what could be the possible test string value such that this ("^") regex will split the test string?

^ 匹配 Java 中行的开头。

根据模式documentation :

By default, the regular expressions ^ and $ ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence. If MULTILINE mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input. When in MULTILINE mode $ matches just before a line terminator or the end of the input sequence. (Emphasis, mine)

因此,以下代码将产生以下输出:

Pattern pattern = Pattern.compile("^", Pattern.MULTILINE);
System.out.println(Arrays.toString(pattern.split("Test\nTest")));

输出:

[Test
, Test]

如您所见,String已成功拆分。

关于java - ^ 正则表达式分割什么样的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53126898/

相关文章:

java - 处理 Java 加密异常

python - 正则表达式用于删除括号后的空格python

regex - 在 Scala 中的给定索引之后查找字符串中的正则表达式匹配

java - 正则表达式帮助在字符串中搜索匹配项

java - 如何在 Java 中将 ArrayList 列表写入 CSV 格式

java - 如何在 TestNG Listener 方法中传递参数 - Selenium Java

java - 文件到流 -> 流到信息 Java

java - 找不到 GOOGLE_APPLICATION_CREDENTIALS

javascript - JavaScript 和正则表达式的问题

regex - 从文本中提取 linkedin 用户名的模式