我想弄清楚一个正则表达式可以用于java的String.split(正则表达式),以便从文件中获取“行”数组。正则表达式满足3个不同的情况
回车没有定义行的结束,而是一个逗号 - 但不是所有的逗号。如果逗号在括号,单引号或注释(/ *注释,更多注释* /)之间,则不表示行的结尾。
例子:
1 test fixed(5,2),
2 another_test char(12),
2 a_third_test,
3 one pic'9{9}V.99',
3 two pic'9,999V.99',
3 three fixed(7,2),
/* test,t*/
/*test 2,*/
/*and more */
2 another_field fixed bin(13),
2 a_really_long_super_long_field_name_requiring_two_lines_for_declaration
char(1),
2 a_field char(8);
预期的输出是(与\ T和额外的空格省略清晰):
1 test fixed(5,2)
2 another_test char(12)
2 a_third_test
3 one pic'9{9}V.99'
3 two pic'9,999V.99'
3 three fixed(7,2)
/* test,t*//*test 2,*//*and more */ 2 another_field fixed bin(13)
2 a_really_long_super_long_field_name_requiring_two_lines_for_declaration
char(1)
2 a_field char(8)
我拿出3个独立的正则表达式来获得3件:
,(?![^(]*\))
- 所有的逗号括号 不
(,(?![^']*'))
- 所有逗号不是单引号(,(?![^\/\*]*\*\/))
- 没有评论所有逗号
我试着用或(.*?)|(,)|'.*?'|(,)|\/*.*?*\/|(,)
加入他们,但得到如下:
1 test fixed
2 another_test char
2 a_third_test
3 one pic
3 two pic
3 three fixed
2 another_field fixed bin
2 a_really_long_super_long_field_name_requiring_a_line_break_... char
2 a_field char
是有一种方法可以将这3个正则表达式(或者是否有更好的表达式)进行组合,以找到满足所有3个的组?
UPDATE:
我可以完成确切的事情了一些简单的Java,但我想用正则表达式来做到这一点作为一个学术化追求。
String temp = "";
for(String line:text.split("\n")){
if(line.trim().charAt(line.trim().length()-1) == ',' || line.trim().charAt(line.trim().length()-1) == ';'){
System.out.println(temp + line);
temp = "";
} else {
temp += line.trim();
}
}
这是或运营商可能帮助? – jdv
这是我尝试使用的第一件事,使用\(。*?\)|(,)|'。*?'|(,)| \/\ *。*?\ * \/|(,) 我得到: '1测试固定 2个another_test焦炭 2 a_third_test 3单张影像 3两PIC 3三个固定 2 another_field固定仓 2 a_really_long_super_long_field_name_requiring_two_lines ...烧焦 2 a_field char' – gooeylewie
这就是应该在问题的主体中的那种东西。 – jdv