基于这个或那个的正则表达式
Regex based on THIS or THAT
我正在尝试解析以下内容:
"#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,第三组返回 ([^=]+=(.+)。| 是什么我正在尝试做。我想根据 EITHER 条件解析它 2nd group
(#.*)
或第三组
([^=]+)=(.+).
如何?
编辑: 错误编写示例代码
您可以使用此正则表达式获取所有 3 个组:
(?m)^(#.*)|^([^=]+)=(.*)
正则表达式分解:
(?m)
:启用MULTILINE
模式
^(#.*)
:匹配组 #1 中以 #
开头的整行
|
: 或
^([^=]+)=
:匹配到 =
并在第 2 组中捕获,然后是 =
(.*)
: 匹配组 #3 中的其余行
我正在尝试解析以下内容:
"#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,第三组返回 ([^=]+=(.+)。| 是什么我正在尝试做。我想根据 EITHER 条件解析它 2nd group
(#.*)
或第三组
([^=]+)=(.+).
如何?
编辑: 错误编写示例代码
您可以使用此正则表达式获取所有 3 个组:
(?m)^(#.*)|^([^=]+)=(.*)
正则表达式分解:
(?m)
:启用MULTILINE
模式^(#.*)
:匹配组 #1 中以 |
: 或^([^=]+)=
:匹配到=
并在第 2 组中捕获,然后是=
(.*)
: 匹配组 #3 中的其余行
#
开头的整行