Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regex non capturing group with subgroups

807589Sep 10 2008 — edited Sep 10 2008
Hi,

I'm having trouble figuring out a regex that should match the following source:

DN   132
     CAND
       CAND_GOOD ROMAN
         NAME SETTINGS
         DISPLAY_FMT FIRST,LAST
TYPE C50
TC   03221 01 051 131  

^Ex1
-----------------------------------------------------
Ex2

DN   20133
TYPE L500
TN   03212 05 127 126  
I would like the regex to match on:
(132)
(NAME) (SETTINGS)
(C50) in ex1.

And
(20133)
(L500) in ex2.

This means that NAME and SETTINGS should be captured only if they exist. So I figured this regex should work:

^DN {1,}(\d+)[\s\S]*?(?:^ {1,}(NAME) ([\w ]+))?[\s\S]*TYPE (\w+)
But the non-capturing group in the middle doesn't want to match it's subgroups when the ? is appended to it and I therefore only get (20133) (C50) in ex1. I would appreciate if someone could help me and explain this behavior.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 8 2008
Added on Sep 10 2008
3 comments
2,831 views