8.2. Option ModifiersSeveral option modifier letters,sometimes called flags,may be appended as a group right after the ending delimiter of a regular expression to change its behavior from the default. 8.2.1. Case-Insensitive Matching with /iTo make a case-insensitive pattern match,so you can match FRED as easily as fred or Fred,use the /i modifier: print "Would you like to play a game? "; chomp($_ = <STDIN>); if (/yes/i) { # case-insensitive match print "In that case,I recommend that you go bowling./n"; } 8.2.2. Matching Any Character with /sBy default,the dot (.) doesn't match newline,and this makes sense for most "look within a single line" patterns. If you have newlines in your strings and want the dot to be able to match them,the /s modifier will do the job. It changes every dot[*] in the pattern to act as the character class [/d/D] does,which is to match any character,even if it is a newline. You have to have a string with newlines for this to make a difference:
$_ = "I saw Barney/ndown at the bowling alley/nwith Fred/nlast night./n"; if (/Barney.*Fred/s) { print "That string mentions Fred after Barney!/n"; } Without the /s modifier,that match would fail since the two names aren't on the same line. 8.2.3. Adding Whitespace with /xThe /x modifier allows you to add arbitrary whitespace to a pattern to make it easier to read. /-?/d+/.?/d*/ # what is this doing? / -? /d+ /.? /d* /x # a little better Since /x allows whitespace inside the pattern,a literal space or tab character within the pattern is ignored. You Could use a backslashed space or /t (among many other ways) to match these,but using /s (or /s* or /s+) is more common when you want to match whitespace anyway. In Perl,comments may be included as part of the whitespace,so with /x,we can put comments into that pattern to tell what it's doing: / -? # an optional minus sign /d+ # one or more digits before the decimal point /.? # an optional decimal point /d* # some optional digits after the decimal point /x # end of pattern Since the pound sign indicates the start of a comment,use /# or [#] in the rare case that you need to match a pound sign. And be careful not to include the closing delimiter inside the comments,or that will prematurely terminate the pattern. 8.2.4. Combining Option ModifiersIf you have more than one option modifier to use on the same pattern,they may be used one after the other. Their order isn't significant: if (/barney.*fred/is) { # both /i and /s print "That string mentions Fred after Barney!/n"; } For a more expanded version with comments: if (m{ barney # the little guy .* # anything in between fred # the loud guy }six) { # all three of /s and /i and /x print "That string mentions Fred after Barney!/n"; } Note the shift to curly braces here for the delimiters as well,allowing programmer-style editors to easily bounce from the beginning to the ending of the regular expression. 8.2.5. Other OptionsMany other option modifiers are available. We'll cover those as we get to them,or you can read about them in the perlop manpage and in the descriptions of m// and the other regular expression operators that you'll see later in this chapter. |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。