in reply to regular expressions

The following code shows some variations on what you want. It matches and captures a string containing one or more no white space characters(represented by \S). The various options show the difference between greedy and non-greedy matching as well as anchoring on word boundaries.
#! /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"; }