in reply to Regular Expressions: Call for Examples

I would nominate two regexes that merlyn was responsible for, one being the answer to "I want to replace spaces with underscores, but only where they're found between brackets" on a newsgroup posting somewhere.

It's a relatively simple one, but it opened my eyes, as a beginner, to a whole world of nested and executing regexes.

I'm reproducing it here from memory so merlyn will forgive me if it's not quite how he did it:

$str= 'no change <these spaces need replacing> not these <these do>'; $str =~ s{(<[^>]*?>)} { my $x=$1; $x=~s/ /_/g; $x; }egx; print $str

And the other one I can't remember at all, but I remember it involved Old MacDonald, and a regex that double-executed, and therefore ended "/eieio". Has to be included.
--

($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
Re: Re: Regular Expressions: Call for Examples
by footpad (Abbot) on Jul 23, 2002 at 22:11 UTC

    Do you mean this one?

    $Old_MacDonald = q#print #; $had_a_farm = (q-q:Just another Perl hacker,:-); s/^/q[Sing it, boys and girls...],$Old_MacDonald.$had_a_farm/eieio;

    As far as I can tell, the first (currently) recorded appearance of this can be found on this announcement regarding what I presume is the first edition of a certain book in most of our collections.

    (Note: I've written as three lines because the two-line version wrapped oddly.)

    --f