满足 3 个不同情况的正则表达式
Regex which satisfies 3 separate cases
我正在尝试找出可以与 java 的 String.split(regex) 一起使用的正则表达式,以便从文件中获取 "lines" 的数组。
回车 return 不定义行尾,而是逗号 - 但不是所有逗号。如果逗号位于括号、单引号或注释(/*注释,更多注释*/)之间,则不表示行结束。
示例:
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 个部分:
,(?![^(]*\))
- 所有不在括号中的逗号
(,(?![^']*'))
- 所有逗号不在单引号中
(,(?![^\/\*]*\*\/))
- 所有逗号不在评论中
我尝试用 or (.*?)|(,)|'.*?'|(,)|\/*.*?*\/|(,)
加入他们,但得到以下结果:
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 个的组?
更新:
我可以用一些简单的 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();
}
}
我想你可能想多了。重要的是要记住,正则表达式是为解析 regular languages. When you need to check if you're inside a comment or parens or whatever else to know what a comma means, what you're looking at is a context-sensitive language 而创建的(见下图)。
By J. Finkelstein (Own work) [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)
也就是说,匹配行尾的逗号和分号很容易。 /\s*(.*?)[,;]$/gsm
适用于您问题中的测试输入。但是,这没有考虑
test fixed(5,2),
/* a,
multi-line,
comment,
*/
在我看来,解决此问题的最佳选择是在开始使用 \/\*.*?\*\/
进行解析之前丢弃注释。如果你需要保留评论,你可能会使用负面的环视,但这些是非常低效的,你最好写一个 tokenizer/parser.
我正在尝试找出可以与 java 的 String.split(regex) 一起使用的正则表达式,以便从文件中获取 "lines" 的数组。
回车 return 不定义行尾,而是逗号 - 但不是所有逗号。如果逗号位于括号、单引号或注释(/*注释,更多注释*/)之间,则不表示行结束。
示例:
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 个部分:
,(?![^(]*\))
- 所有不在括号中的逗号(,(?![^']*'))
- 所有逗号不在单引号中(,(?![^\/\*]*\*\/))
- 所有逗号不在评论中
我尝试用 or (.*?)|(,)|'.*?'|(,)|\/*.*?*\/|(,)
加入他们,但得到以下结果:
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 个的组?
更新:
我可以用一些简单的 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();
}
}
我想你可能想多了。重要的是要记住,正则表达式是为解析 regular languages. When you need to check if you're inside a comment or parens or whatever else to know what a comma means, what you're looking at is a context-sensitive language 而创建的(见下图)。
By J. Finkelstein (Own work) [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)
也就是说,匹配行尾的逗号和分号很容易。 /\s*(.*?)[,;]$/gsm
适用于您问题中的测试输入。但是,这没有考虑
test fixed(5,2),
/* a,
multi-line,
comment,
*/
在我看来,解决此问题的最佳选择是在开始使用 \/\*.*?\*\/
进行解析之前丢弃注释。如果你需要保留评论,你可能会使用负面的环视,但这些是非常低效的,你最好写一个 tokenizer/parser.