[英]Regex based on THIS or THAT
I am trying to parse the below: 我试图解析下面的内容:
"#SenderCompID=something\n" +
"TargetCompID=something1"
into an array of: 成阵列:
{"#SenderCompID=something", "TargetCompId", "something1"}
Using: 使用:
String regex = "(?m)" + "(" +
"(#.*) |" + //single line of (?m)((#.*)|([^=]+=(.+))
"([^=]+)=(.+) + ")";
String toMatch = "#SenderCompID=something\n" +
"TargetCompID=something1";
which is outputting: 正在输出:
#SenderCompID=something
null
#SenderCompID
something
//why is there any empty line here?
TargetCompID=something1
null
//why is there an empty line here?
TargetCompID
something1
I understand what I'm doing wrong here. 我明白我在这里做错了什么。 The 1st group is returning the entire line, the 2nd group is returning (#.*) if the line starts with # and null otherwise, the 3rd group is returning ([^=]+=(.+). The | is what I'm trying to do. I want to parse it based on EITHER the conditions for the 2nd group
第一组返回整行,第二组返回(#。*)如果行以#开头,否则返回null,第三组返回([^ =] + =(。+)。|是什么我想要做的事。我想基础上无论是对第二组的条件来分析它
(#.*)
or for the 3rd group 或第三组
([^=]+)=(.+).
How? 怎么样?
EDIT: miswrote example code 编辑:错误编写示例代码
You can use this regex to get all 3 groups: 您可以使用此正则表达式来获取所有3个组:
(?m)^(#.*)|^([^=]+)=(.*)
RegEx Breakup: RegEx分手:
(?m)
: Enable MULTILINE
mode (?m)
:启用MULTILINE
模式 ^(#.*)
: match a full line starting with #
in group #1 ^(#.*)
:匹配组#1中以#
开头的整行 |
: OR ^([^=]+)=
: Match till =
and capture in group #2 followed by =
^([^=]+)=
:匹配到=
并在组#2中捕获,然后是=
(.*)
: Match rest of line in group #3 (.*)
:匹配组#3中的其余行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.