我正在尝试解析以下内容:
"#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
我了解我在这里做错了。第一组返回整行,如果行以#开头,第二组返回(#。*),否则返回null,否则第三组返回([^ =] + =(。+)。我正在尝试做,我想根据第二组的条件来分析它
(#.*)
或第三组
([^=]+)=(.+).
怎么样?
编辑:错误编写示例代码
最佳答案
您可以使用此正则表达式获取所有3个组:
(?m)^(#.*)|^([^=]+)=(.*)
RegEx Demo
正则表达式分解:
(?m)
:启用MULTILINE
模式^(#.*)
:匹配#1组中以#
开头的整行|
:或^([^=]+)=
:匹配至=
并在#2组中捕获,然后在=
中捕获(.*)
:匹配组3中的其余行