如果我有类似的 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. IfMULTILINE
mode is activated then^
matches at the beginning of input and after any line terminator except at the end of input. When inMULTILINE
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/