Java正则表达式 - 条件分组

时间:2015-09-08 10:27:35

标签: java regex regex-lookarounds

我需要解析两组中的以下几行:
第1组:ADJ-1-37-10
第2组:3.00(如果没有SPANLOSSMARGIN标签那么它应该是空的)

到目前为止,我使用"(ADJ-\d{1,3}-\d{1,3}-\d{1,3})::.*?SPANLOSSMARGIN=(\d{1,2}.\d{1,2})来解析以下行。但由于该行不包含SPANLOSSMARGIN标记,因此它与ADJ-1-37-16不匹配。我的要求是当有ADJ-X-XX-XX标签(不管是否存在其他标签)时,第1组应该返回它,当有SPANLOSSMARGIN标签时,第2组应该返回它;但只有当没有SPANLOSSMARGIN标记时,Group 2才会返回空。我怎样才能做到这一点?

"ADJ-1-2-5::FIBERTYPE=NDSF,OSCSPANLOSS=16.00,TARGSPANLOSS=16.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00"
   "ADJ-1-13-5::FIBERTYPE=NDSF,OSCSPANLOSS=16.00,TARGSPANLOSS=16.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00"
;
   "ADJ-1-37-2::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-4::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-8::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-10::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-14::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-16::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-20::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""
   "ADJ-1-37-22::FIBERTYPE=UNKNOWN,TARGSPANLOSS=0.00,MINSPANLOSS=0.00,SPANLOSSMARGIN=3.00,SPANLOSSSOURCE=UNKNOWN,BUNDLEID=\"\",LINEIN=\"\",LINEOUT=\"\",CRSIN=\"\",CRSOUT=\"\""

1 个答案:

答案 0 :(得分:1)

试试这个:

(ADJ-\d{1,3}-\d{1,3}-\d{1,3})::(.(SPANLOSSMARGIN=)?(\d{1,2}.\d{1,2})?)*

你可以在这里试试:

http://fiddle.re/qm6j86

第4组是SPANLOSSMARGIN=的匹配,如果SPANLOSSMARGIN=不存在,则第3组将是null