$" = "\n"; # so the array printouts have linebreaks between elements $string = 'start at the beginning and go on to till the end'; if( $string =~ /^start.*end$/ ){ print 'true' } else { print 'false'} print "\n\n"; # True, obviously, because the string starts with # the character sequence 'start' and ends with 'end' $string = 'start at the beginning and go on till the end'; if( $string =~ /^start.*end$/ ){ print 'true' } else { print 'false'} print "\n\n"; # False, because now there's a newline in the middle. # It still starts with 'start' and ends with 'end', but # the dot-star part in the middle doesn't match the # newline in the middle. If you want to find this # kind of match, add the /s modifier. if( $string =~ /^start.*end$/s ){ print 'true' } else { print 'false'} print "\n\n"; # Now it's true. Dot is allowed to match newlines with /s modifier. # Make the string into a force-wrapped paragraph of text # which matches the pattern as a whole string, but also # contains *lines* which match the pattern and try to # put them all into an array, using the /g modifier. $string = 'start at the beginning, tell the story of how you started a company with your friend and how it collapsed, part of the startup-company-collapse trend of the nineties, and go on till the end'; if( @matches = $string =~ /^start.*end$/g ){ print "Matches: @matches" } else { print 'no matches' } print "\n\n"; # No matches. Not even one, but they should be expecting that, # because, no /s modifier. # What happens if you add the /s modifier? How many matches? if( @matches = $string =~ /^start.*end$/gs ){ print "Matches with s modifier:\n@matches" } else { print 'no matches' } print "\n\n"; # @matches has one item, the entire string. # The *entire string* matches because the entire string # has only one start point (found by the ^ marker) and only one # end point (found by the $ marker) and dot matches newlines. # If you want the ^ marker and the $ marker to look at the # start point and end point of *lines*, not at the start point # and end point of the *string*, then you need the /m modifier. if( @matches = $string =~ /^start.*end$/gm ){ print "Matches with m modifier:\n@matches" } else { print 'no matches' } print "\n\n"; # Add the /s to it and it again matches the whole string: if( @matches = $string =~ /^start.*end$/gsm ){ print "Matches with m modifier:\n@matches" } else { print 'no matches' } print "\n\n"; # So what would be the point of ever having them # together? It doesn't make sense here, but # it does if you make dot-star non-greedy: if( @matches = $string =~ /^start.*?end$/gsm ){ print "Matches with m modifier:\n@matches" } else { print 'no matches' } print "\n\n"; # This version finds *two* matches, one from the very start # down to the word "friend", then another one, the single line # "startup-company-collapse trend", because the question mark # made the regex find the nearest "end" not the farthest each # time.