匹配两个字符,中间有多行(matching two chars with multiple lines in between)

我是正则表达式的新手,我正在使用Perl。

我有以下标签:

<CFSC>cfsc_service=TRUE SEC=1 licenses=10 expires=20170511 </CFSC>

我想匹配<CFSC>和</CFSC>标签之间的任何内容。

我试过/<CFSC>.*?\n.*?\n.*?\n.*?\n<\/CFSC>/和/<CFSC>(.*)<\/CFSC>/但没有运气。

I am new to regex and I am using Perl.

I have below tag:

<CFSC>cfsc_service=TRUE SEC=1 licenses=10 expires=20170511 </CFSC>

I want to match anything between <CFSC> and </CFSC> tags.

I tried /<CFSC>.*?\n.*?\n.*?\n.*?\n<\/CFSC>/ and /<CFSC>(.*)<\/CFSC>/ but had no luck.

最满意答案

您需要使用/s 单行修饰符来使正则表达式引擎包含换行符. 。

将字符串视为单行。 也就是说,改变“。” 匹配任何字符,甚至是换行符,通常它不匹配。

看这个例子。

my $foo = qq{<CFSC>cfsc_service=TRUE SEC=1 licenses=10 expires=20170511 </CFSC>}; $foo =~ m{>(.*)</CFSC>}s; print $1;

您还需要使用与/不同的分隔符,或者将其转义。

You need the /s single line modifier to make the regex engine include line breaks in ..

Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

See this example.

my $foo = qq{<CFSC>cfsc_service=TRUE SEC=1 licenses=10 expires=20170511 </CFSC>}; $foo =~ m{>(.*)</CFSC>}s; print $1;

You also need to use a different delimiter than /, or escape it.

更多推荐