#!/usr/bin/perl -w ############### # # Some idomatic uses of regexps in perl # ################ use strict; print "Content-type: text/plain\n\n"; # Define some text. my $text = " This is some text 123 with 01234 balloon bookkeeper feet words "; print " text = $text \n"; # Substitue first instance of 123 for 456 in a copy of that text. (my $new = $text) =~ s/123/456/; print " new = $new \n"; # Set the $_ variable. Look for digits. # In scalar context, return 1 or 0 for success or failure. for ($new) { my $boolean = /(\d+)/; print " scalar context : $boolean \n"; } # Look for digits in the string $new. # In array context, we return a list of the successful matches. # The first one gets put in the first element of the list. # Here are two forms similar forms. (my $firstmatch) = ( $new =~ m/(\d+)/ ); # return first match only my ($first, $second) = $new =~ m/(\d+)/g; # note the "g" for global print " firstmatch, array context : $firstmatch \n"; print " first, second array context : $first $second \n"; # Look for words with two doubled characters. # Here's the dense version of what the \x allows us to spread out # into a (somewhat) more readable form. # my @allmatches = $text =~ m|(\w*(.)\2(.)\3\w*)|g ; # # Note that since the engine doesn't backtrack, we only find # bookkeeper once. # my @allmatches = ( $text =~ m| ( # start remembering \1 \w* # beginning of word (.)\2 # any char followed by itself (.)\3 # a second doubled character \w* # rest of word ) # end of word \1 |gx # everywhere, extended syntax ); print " allmatches = " , join("|", @allmatches), "\n"; # If we only wanted the words, we could print # just every third entry in @allmatches. while (@allmatches) { print " found a word with two double chars : $allmatches[0] \n"; shift @allmatches for 1..3; }