I don't quite know where on PerlMonks this belongs, but I'll put it here and let the admins decide.
--
I read Japhy's draft chapters of his book on regular expressions and wrote to him with some suggestions, (as he invited readers to do) for examples of the /s and /m modifiers and what they do.
I thought I'd post them here as well, in case other monks wanted to comment or add to them:
$" = "\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.
--
Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.
M-J D
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: S And M -- modifiers, that is...
by davorg (Chancellor) on Feb 25, 2003 at 11:05 UTC | |
by thelenm (Vicar) on Feb 26, 2003 at 21:41 UTC | |
by Anonymous Monk on Jun 22, 2005 at 18:30 UTC | |
by Cody Pendant (Prior) on Feb 25, 2003 at 21:51 UTC | |
Re: S And M -- modifiers, that is...
by Anonymous Monk on Feb 25, 2003 at 08:00 UTC | |
by Cody Pendant (Prior) on Feb 25, 2003 at 21:47 UTC |
Back to
Meditations