perl 跨行匹配;

来源:互联网 发布:少儿编程课程 编辑:程序博客网 时间:2024/06/05 11:16
<pre name="code" class="html"><pre name="code" class="html">perl  跨行匹配;6.6. Matching Within Multiple Lines6.6.1. ProblemYou 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. SolutionUse /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.多行匹配6.6.1 问题你需要使用正则表达式在一个自付出啊吧 包含多个逻辑行, 但是特定的字符串(任何字符串除了换行) ^ 表示字符串开始$ 字符串结束 似乎不能为你工作 。这个可能发生如果你读取多行 记录 6.6.2 解决使用/m ,/s  或者两者  作为模式修改 /s 允许. 匹配一个换行(通常它不能)  如果目标字符串有多余一行,/foo,*bar/s  可以匹配一个"foo 在一行和一个"bar" 在另外一行  这个不会影响点号 在字符类 the "." metacharacter matches any character except    "\n" (unless you use "/s")/m 修饰符 允许 ^和$来立即匹配 之前和之后一个嵌入的换行符,分别的,/^=head[1-7]/m会匹配 模式不是在记录的开始,但是任何右边在一个新行/s 当作一行处理/s 令 . 匹配换行符并且忽略不建议使用的 $* 变量/m 令 ^ 和 $ 匹配下一个嵌入的 \n。zjtest7-frontend:/root# cat a2.pl my $str="sdgssdgdsgbeginddd...end d,,,,sdsdgbegin sdsgend ";if ($str =~/.*?begin.(.*).end/s){    print "\$1 is $1\n";   };   zjtest7-frontend:/root# perl a2.pl $1 is ddd...end d,,,,sdsdgbegin sdsgThe /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.


                                             
0 0
原创粉丝点击