+ -
当前位置:首页 → 问答吧 → perl正则中后缀修饰符 /s和/m有什么区别?

perl正则中后缀修饰符 /s和/m有什么区别?

时间:2011-02-11

来源:互联网

/s使.能够匹配\n ,那么可以跨行匹配文本,/m呢?

作者: justlooks   发布时间: 2011-02-11

m 是将字符串作为多行处理,s是将字符串作为单行处理,如果是s在字符串中出现的\n就相当于普通字符。

作者: zzy7186   发布时间: 2011-02-11

本帖最后由 justlooks 于 2011-02-11 09:56 编辑

回复 zzy7186


理解了

作者: justlooks   发布时间: 2011-02-11

perldoc perlre 第一小节讲了所有修饰符 "msgixcp"

作者: zhlong8   发布时间: 2011-02-11

回复 justlooks


   

QUOTE:
6.6. Matching Within Multiple Lines
6.6.1. Problem
You want to use regular expressions on a string containing more than one logical line, but the special characters . (any character but newline), ^ (start of string), and $ (end of string) don't seem to work for you. This might happen if you're reading in multiline records or the whole file at once.

6.6.2. Solution
Use /m, /s, or both as pattern modifiers. /s allows . to match a newline (normally it doesn't). If the target string has more than one line in it, /foo.*bar/s could match a "foo" on one line and a "bar" on a following line. This doesn't affect dots in character classes like [#%.], since they are literal periods anyway.

The /m modifier allows ^ and $ to match immediately before and after an embedded newline, respectively. /^=head[1-7]/m would match that pattern not just at the beginning of the record, but anywhere right after a newline as well.

作者: Perl_Er   发布时间: 2011-02-11

对的

作者: justlooks   发布时间: 2011-02-11