in reply to Regex Subexpressions

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 ... ;->

Replies are listed 'Best First'.
Re^2: Regex Subexpressions
by BenjiSmith (Novice) on Sep 10, 2005 at 00:49 UTC
    Yep, I'm a java programmer. I've been programming in Java for about five years, but I've also been writing Perl for the last three years. I just picked up Python a few weeks ago, and I really like it a lot.

    I've also done brief stints with VBA, D, C++, and x86 Assembly.

    But I know that--when I have a difficult regex problem--the people at perlmonks are more likely to be able to figure out a solution than anyone else.
      But I know that--when I have a difficult regex problem--the people at perlmonks are more likely to be able to figure out a solution than anyone else.
      I consider that a misuse, perhaps even an abuse, of the openness here.

      That'd be like saying "I know that most people here probably have apple computers, so I'll post my apple questions here". Seeing questions about Java, with Java's restrictions (even if it is regex), is not the reason I come here. Your task was simple with Perl's regex. Java's regex are different, and thus the answers weren't relevant, but you didn't disclose that at the beginning.

      Please mark your questions "off topic" next time, so I can ignore them.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.