generic regex to capture various optional groups
generic regex to capture various optional groups I am looking for a method to select, using regex, lines containing various groups, some of them optional, and capture the groups - the found ones, of course. After reading here on stackoverflow and many experiments I came up with this general enough approach: ^.*?(?:.*?(aaa).*?|.*?).*?(xxx).*?(yyy).*?(?:.*?([^ n]+).*?|.*?).*?$ So the general term for optional groups is: (?:.*?(blabla).*?|.*?) The above approach has backtracking problems in case of: Any ideea how to create a generic enough regex able to capture optional groups? By generic I mean, like in the example I found, easily scalable up for various group patterns. Thanks. Hi. Please include a tag for one specific engine or language (perl, pcre, c#, etc…). Regular expression questions get better answers if they… show the pattern that isn't working, provide some examples of input text that should match, and also ones that shouldn't match . Describe...