基于文本限定符regex java拆分字符串

时间:2015-06-18 15:59:56

标签: java regex split

我想基于文本限定符分割字符串,例如

"1","10411721","MikeTison","08/11/2009","21/11/2009","2800.00","002934538","051","New York","10411720-002",".\Images\b.jpg",".\RTF\b.rtf"

限定符= " Spliter = ,

我想基于Spliter ,拆分字符串,但是如果Spliter在限定符"内部而不是忽略它并返回包含Spliter的字符串。

我正在使用的正则表达式为(?:|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)

但是这个正则表达式只返回逗号,请从这个角度帮助我,因为我是正则表达式的新手

请注意,如果我们在字符串中有换行符,即\r\n,则应忽略换行符

"1","10411","Muis","a","21/11/2009","2800.06","0029683778","03005136851","Awan","10411720-001",".\Images\a.jpg",".\RTF\a.rtf"
"2","08/10/2009","07:32","Call","On-Net","030092343242342376543","Monk","00:00","1.500","0.000","10.000","0.200"
"2","08/10/2009","02:50","Call","Off-Net","030092343242342376543","Une","08:00","1.500","2.000","20.000","3.500"
"2","09/10/2009","03:55","SMS","On-Net","030092343242342376543","Mink","00:00","1.500","0.000","5.000","100.500"
"2","09/10/2009","12:30","Call","Off-Net","030092343242342376543","Zog","01:01","3.500","3.000","70.000","6.500"
"2","09/10/2009","09:11","Call","On-Net","030092343242342376543","Monk","02:30","2.00","2.000","90.000","4.000"

4 个答案:

答案 0 :(得分:1)

可能最简单的解决方案不是搜索split的位置,而是搜索要返回的find元素。在你的情况下这些元素

  • 启动"
  • "
  • 结尾
  • 里面没有"

所以你试试像

这样的东西
String data = "\"1\",\"10411721\",\"MikeTison\",\"08/11/2009\",\"21/11/2009\",\"2800.00\",\"002934538\",\"051\",\"New York\",\"10411720-002\",\".\\Images\\b.jpg\",\".\\RTF\\b.rtf\"";

Pattern p = Pattern.compile("\"([^\"]+)\"");
Matcher m = p.matcher(data);
while(m.find()){
    System.out.println(m.group(1));
}

输出:

1
10411721
MikeTison
08/11/2009
21/11/2009
2800.00
002934538
051
New York
10411720-002
.\Images\b.jpg
.\RTF\b.rtf

答案 1 :(得分:0)

删除整个字符串的第一个和最后一个字符。然后用","

分开
outputVideo.open(name, CV_FOURCC('M', 'J', 'P', 'G'), 25, size, true); // create a new videoFile with 25fps

答案 2 :(得分:0)

您可以使用此正则表达式进行拆分:

String[] arr = input.split( "(?=(([^\"]*\"){2})*[^\"]*$),+" );

如果这些正则表达式使用前瞻以确保在逗号后面有偶数引号,那么这些正则表达式将在逗号上分割,如果它们在双引号之外。

答案 3 :(得分:0)

即使您有新的线条字符,它仍然有效。

    String str="\"1\",\"10411721\",\"MikeTison\",\"08/11/2009\",\"21/11/2009\",\"2800.00\",\"002934538\",\"051\",\"New York\",\"10411720-002\",\".\\Images\\b.jpg\",\".\\RTF\\b.rtf\"";
    System.out.println(Arrays.toString(str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")));