Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拆分一个字符串,其中分隔字符串^^^^^对正则表达式有特殊意义_Java_Regex - Fatal编程技术网

Java 拆分一个字符串,其中分隔字符串^^^^^对正则表达式有特殊意义

Java 拆分一个字符串,其中分隔字符串^^^^^对正则表达式有特殊意义,java,regex,Java,Regex,试着像这样分开一根线 12^^^^John Doe^^^^+1897987987 delimiter is the ^^^^ String[] line = str.split("^^^^"); id = line[0]; // code below does not work because there is only one element in array name = line[1]; number = line[2]; split方法不进行拆分,而是

试着像这样分开一根线

12^^^^John Doe^^^^+1897987987
delimiter is the ^^^^
   String[] line = str.split("^^^^");
   id = line[0];
   // code below does not work because there is only one element in array 
   name = line[1];
   number = line[2];
split方法不进行拆分,而是返回整行和单长度数组

   String[] line = str.split("^^^^");
   id = line[0];
   // code below does not work because there is only one element in array 
   name = line[1];
   number = line[2];

^在正则表达式中有特殊的含义,因此我认为必须以不同的方式传递此拆分参数才能达到预期的结果,请告知。

您需要转义这些字符-
^
表示正则表达式中的“行开始”

   String[] line = str.split("^^^^");
   id = line[0];
   // code below does not work because there is only one element in array 
   name = line[1];
   number = line[2];
String[]line=str.split(\\^\\\^\\\^^)

   String[] line = str.split("^^^^");
   id = line[0];
   // code below does not work because there is only one element in array 
   name = line[1];
   number = line[2];
需要两个
\
,否则Java会尝试将
\^
解释为字符串转义字符,如
\n
\t

   String[] line = str.split("^^^^");
   id = line[0];
   // code below does not work because there is only one element in array 
   name = line[1];
   number = line[2];