我有一个.txt输入文件如下:
Start "String" (100, 100) Test One:
Nextline 10;
Test Second Third(2, 4, 2, 4):
String "7";
String "8";
Test "";
End;
End.
我打算将此文件读为一个字符串,然后根据某些定系数将其拆分。我几乎使用此代码遇到了所需的输出:
String tr= entireFile.replaceAll("\s+", "");
String[] input = tr.split("(?<=[(,):;.])|(?=[(,):;.])|(?=\p{Upper})");
我当前的输出是:
Start"
String"
(
100
,
100
)
Test
One
:
Nextline10
;
Test
Second
Third
(
2
,
4
,
2
,
4
)
:
String"7"
;
String"8"
;
Test""
;
End
;
End
.
但是,我在报价中处理物品或只是简单的引号"作为单独的令牌。因此,"字符串"one_answers" 7"one_answers"应该在单独的线上。有没有办法使用正则表达式?我的预期输出在下面,感谢您的任何帮助。
Start
"String"
(
100
,
100
)
Test
One
:
Nextline
10
;
Test
Second
Third
(
2
,
4
,
2
,
4
)
:
String
"7"
;
String
"8"
;
Test
""
;
End
;
End
.
这是我想到的正则:
String[] input = entireFile.split(
"\s+|" + // Splits on whitespace or
"(?<=\()|" + // splits on the positive lookbehind ( or
"(?=[,).:;])|" + // splits on any of the positive lookaheads ,).:; or
"((?<!\s)(?=\())"); // splits on the positive lookahead ( with a negative lookbehind whitespace
了解所有正面/负面的lookahead/lookBehind术语,请看一下这个答案。
请注意,您应该直接将此拆分应用于输入文件,而无需删除空格,又名删除此行:
String tr= entireFile.replaceAll("\s+", "");