Re: A Friday Regex Head Scratcher
"Dave O'Neill" <[email protected]>
| Newsgroups | gmane.user-groups.linux.ottawa.general |
|---|---|
| Message-ID | <[email protected]> |
[email protected] wrote: > $string =~ s[ (?<=PR\s) # preceeded by 'PR ' > \s*\d+\s*(,\s*\d+\s*)* > ][ > join( ', ', > map { > s/^\s+|\s+$//g; # trim whitespaces > "<a href='http://$_'>$_</a>"; # url-ify > } > split ',' => $& > ) > . ' ' # add a whitespace after the list > ]xeg; Using $& is generally a bad idea, because using it once imposes a performance penalty on all other regular expression matches. With a slight modification, you can avoid the performance hit by placing the capturing parens around the entire list of digits, and using non-capturing parens for the internal grouping: $string =~ s[ (?<=PR\s) # preceeded by 'PR ' \s*(\d+\s*(?:,\s*\d+\s*)*) ][ join( ', ', map { s/^\s+|\s+$//g; # trim whitespaces "<a href='http://$_'>$_</a>"; # url-ify } split ',' => $1 ) . ' ' # add a whitespace after the list ]xeg; Unfortunately, this isn't going to help him in Python... Perl-compatible regular expressions aren't compatible to the point of letting you run perl code with the /e modifier :) Cheers, Dave -- OCLUG general discussion list [email protected] http://oclug.on.ca/mailman/listinfo/oclug