in reply to regular expressions
#! /usr/bin/perl -w use strict; use warnings; my $data = "abcdefghijklmnopFqrstuvwxyz arrrrhhhhg!"; # Greedily match as much non-whitespace (\S+) as we can that # starts with an a and ends with an f or g print "Greedily matched $1\n" if $data =~ /(a\S+(f|g))/i; # Add the ? modifier to make the expression match minimally print "Minimally matched $1\n" if $data =~ /(a\S+?(f|g))/i; # Anchor each end of the expression to a word boundary (\b) # so that we only match words that start with an a and ends with an f +or g print "Word matched $1\n" if $data =~ /(\ba\S+(f|g)\b)/; # Apply the same regular expression to pick out all of the # matches in the data while ($data =~ /(a\S+?(f|g))/ig) { print "Word: $1\n"; }
|
|---|