<pre name="code" class="html"><pre name="code" class="html">perl 跨行匹配;
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.
多行匹配
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="sdgs
sdgdsg
begin
ddd
...
end
d,,,,
sdsdg
begin
sdsg
end ";
if ($str =~/.*?begin.(.*).end/s){
print "\$1 is $1\n";
};
zjtest7-frontend:/root# perl a2.pl
$1 is ddd
...
end
d,,,,
sdsdg
begin
sdsg
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 跨行匹配;
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++ 模板匹配matchTemplate
C++ 模板匹配matchTemplate
opencv 模版匹配 -
【数据结构】详细介绍串的简单模式匹配——朴素模式匹配算法
【数据结构】第四章——串详细介绍串的朴素模式匹配算法……
数据结构 模式匹配 C语言 字符串匹配 -
python re 跨行匹配 python匹配换行符
平常 点 (.)去匹配任意字符的时候,是不能匹配换行符的
python re 跨行匹配 换行符 对换 字符串