2

I need to parse the following lines in 2 groups:
Group 1: ADJ-1-37-10
Group 2: 3.00 (if there's no SPANLOSSMARGIN tag then it should be empty)

So far I used "(ADJ-\d{1,3}-\d{1,3}-\d{1,3})::.*?SPANLOSSMARGIN=(\d{1,2}.\d{1,2}) to parse the following lines. But it's not matching the ADJ-1-37-16 as the line doesn't contain the SPANLOSSMARGIN tag. My requirement is when there's ADJ-X-XX-XX tag (irrespective of the presence of other tags) then Group 1 should return it and when there's is SPANLOSSMARGIN tag then Group 2 should return it; but only when there's no SPANLOSSMARGIN tag, Group 2 should return empty. How can I achieve this?

"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 Answer 1

1

Try this:

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

You can try it here:

http://fiddle.re/qm6j86

Group 4 is the match for SPANLOSSMARGIN= , if SPANLOSSMARGIN= doesn't exists than group 3 will be null

Sign up to request clarification or add additional context in comments.

4 Comments

I had to modify this to (ADJ-\d{1,3}-\d{1,3}-\d{1,3})::(.(SPANLOSSMARGIN=(\d{1,2}.\d{1,2}))?)* as Group 4 was returning 0.00 even if there's no SPANLOSSMARGIN tag. Is it OK?
@surz i said in my answer you need to check group 3 to see if a match was made
you are right, I'm directly using Group 4, so had to tweak a little bit, do you see any issue in this regex?
the one that you modified seems fine to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.