Okay, how about this for a solution?  It doesn't do it all in a single regex (hey, go easy on me; I'm only at monk level), but it does do what I think it is you're asking for ...
#!/usr/bin/perl -w use strict; use warnings; # Prototypes sub findMatches($$); # Input data my @keywordList = ( 'john', 'john.smith', 'john.smith@mail.com' ); # Main program my $searchText = "john's username is john.smith and his email address +is john.smith\@mail.com"; my $pmatches = findMatches($searchText, \@keywordList); map { print "$_\n"; } @$pmatches; # Subroutines # # Inputs: $1 ... the text string to match against # $2 ... a pointer to the list of valid matching substrings # # Outputs: $1 ... a pointer to a list of all matches # sub findMatches($$) { my ($text, $plist) = @_; my @matches; foreach my $pattern (@$plist) { while ($text =~ /($pattern)/g) { my $result = $1; # Got a match in $1 push @matches, $result; # Save it to our master res +ults list # Now trim off the first character (otherwise we'll be mat +ching # against the same substring (think 'deep recursion'), and + call # this subroutine again for recursively generated sub-matc +hes. # Whatever we get (if anything) is added to the list. # $result =~ s/^.//; my $psubstr = findMatches($result, $plist); push @matches, @$psubstr; } } return \@matches; }
By the way, you're a java programmer, right?  I'm not being perjorative or anything (well, maybe a little :-), but I had never heard of a "reluctant qualifier", and when I googled for it, I got mostly Java matches.  That plus the lowerCaseMixedWithUpperCase variable names kinda gave you away ... ;->

In reply to Re: Regex Subexpressions by liverpole
in thread Regex Subexpressions by BenjiSmith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.