在perl中的多行正则表达式中分割一行正则表达式 [英] split one line regex in a multiline regexp in perl

查看:132
本文介绍了在perl中的多行正则表达式中分割一行正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将正则表达式分成多行.我希望我的正则表达式匹配给定的行:

I have trouble spliting my regex in multiple line. I want my regex to match the line given:

* Code "l;k""dfsakd;.*[])_lkaDald"

所以我创建了这个可以正常工作的正则表达式:

So I created this regex which work:

my $firstRegexpr = qr/^\s*\*\s*Code\s+\"(?<Code>((\")*[^\"]+)+)\"/x;

但是现在我想像这样将其拆分为多行(并希望它匹配相同的东西!):

But now I want to split it in multiline like this(and want it to match the same thing!):

my $firstRegexpr = qr/^\s*\*\s*Code\s+\"
(?<Code>((\")*[^\"]+)+)\"/x;

我读到了这件事,但是我在使用它时遇到了麻烦:

I read about this, but I have trouble using it:

/
 ^\s*\*\s*Code\s+\"
 (?<Code>((\")*[^\"]+)+)\"
/x

我的最后一个问题是关于删除perl regex中的内联变量:

My last question is about removing inlining variable in perl regex:

 my $firstRegexpr = qr/^\s*\*\s*Code\s+\"(?<Code>((\")*[^\"$]+)+)\"\$/x;

字符 $] 在正则表达式中作为变量匹配,如何将其定义为变量?

the character $] is matched as a variable in the regex, how to define it not as a variable?

非常感谢您的时间,请提供明确的示例.

Thanks a lot for your time and please provide explicit example.

推荐答案

x 标志的作用很简单,就是说忽略空白".

What the x flag does is very simply say 'ignore whitespace'.

因此,您不再匹配 '空格'字符,而是必须使用 \ s 或类似的字符.

So you no longer match 'space' characters , and instead have to use \s or similar.

所以你可以这样写:

if ( m/
        ^
        \d+\s+
        fish:\w+\s+
        $ 
      /x ) {
    print "Matched\n";
}

您可以在各种网站上测试正则表达式,但是一个示例是 https://regex101.com/

You can test regular expressions with various websites but one example is https://regex101.com/

以您的示例为例: https://regex101.com/r/eG5jY8/1

但是你的怎么不工作?

此匹配项:

my $string = q{* Code "l;k""dfsakd;.*[])_lkaDald"};

my $firstRegexpr = qr/^\s*
                        \*
                       \s*
                       Code\s+
                       \"
                       (?<Code>((\")*[^\"]+)+)
                       \"
                    /x;

print "Compiled_Regex: $firstRegexpr\n";
print "Matched\n" if ( $string =~ m/$firstRegexpr/ );

关于没有 $] 的问题-有两个答案.或者:使用 \ 对其进行转义,或使用 \ Q \ E .

And as for not having $] - there's two answers. Either: Use \ to escape it, or use \Q\E.

这篇关于在perl中的多行正则表达式中分割一行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆