【发布时间】:2017-07-24 21:09:59
【问题描述】:
我正在尝试解析以下内容:
"#SenderCompID=something\n" +
"TargetCompID=something1"
到一个数组中:
{"#SenderCompID=something", "TargetCompId", "something1"}
使用:
String regex = "(?m)" + "(" +
"(#.*) |" + //single line of (?m)((#.*)|([^=]+=(.+))
"([^=]+)=(.+) + ")";
String toMatch = "#SenderCompID=something\n" +
"TargetCompID=something1";
正在输出:
#SenderCompID=something
null
#SenderCompID
something
//why is there any empty line here?
TargetCompID=something1
null
//why is there an empty line here?
TargetCompID
something1
我明白我在这里做错了什么。第 1 组返回整行,第 2 组返回 (#.*),如果行以 # 开头,否则返回 null,第 3 组返回 ([^=]+=(.+)。| 是什么我正在尝试。我想根据 EITHER 第二组
的条件来解析它(#.*)
或第三组
([^=]+)=(.+).
怎么做?
编辑:错误的示例代码
【问题讨论】:
-
您在问题中提到了管道字符,但没有解释您遇到的问题。
标签: java regex token delimiter