如何编写长正则表达式以适合屏幕?

发布于 2024-07-25 07:47:23 字数 309 浏览 1 评论 0 原文

我在 Perl 中有匹配正则表达式。 分布超过一行的匹配句子。

我意识到如果我展开,我必须仅在一行中输入匹配正则表达式 多行失败:

$array_11 =~ m{By Steve (.*), MarketWatch LONDON (.*) -- Shares of Anglo American rallied on Monday morning as (.*) bet that the mining group will reject a (.*)};'

如果我将其写在多行中,它将无法匹配该字符串。

I have match regular expresion in Perl.
The match sentence that spreads over more than one line.

I realize that I must enter the match regular expresion in one line only, if I spread
to multiple lines it fails:

$array_11 =~ m{By Steve (.*), MarketWatch LONDON (.*) -- Shares of Anglo American rallied on Monday morning as (.*) bet that the mining group will reject a (.*)};'

If I write it in multiple lines it won't able to match this string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-08-01 07:47:23

如前所述,您似乎正在寻找 x 修饰符。
该修饰符会忽略正则表达式中的所有空格,并允许注释(以 # 开头)。

就你而言,它有点丑陋,因为你必须替换所有空格
你确实想在正则表达式中通过 [ ]、\s 或 \s+ 进行匹配:

$array_11 =~ m{By \s+ Steve \s+ (.*), \s+
               MarketWatch \s+ LONDON \s+ (.*) \s+
               -- \s+ Shares \s+ of \s+ Anglo \s+ American \s+ 
               rallied \s+ on \s+ Monday \s+ morning \s+ as \s+ 
               (.*) \s+ bet \s+ that \s+ the \s+ mining \s+ 
               group \s+ will \w+ reject \w+ a \w+(.*)
              }x;

所以事实上我可能会写这样的内容:

my $sentence= q{By Steve (.*), MarketWatch LONDON (.*) }
            . q{-- Shares of Anglo American rallied on Monday morning as (.*) }
            . q{bet that the mining group will reject a (.*)}
            ;
my $array_11=~ m{$sentence};

最后一条评论:$array_11 有很强的代码味道,如果它是一个数组,然后使其成为一个数组,而不是几个标量变量。

As mentioned previously, it looks like you are looking for the x modifier.
That modifier ignores all whitespaces in the regexp, and allow comments (starting with #).

In your case it's a bit ugly though, because you then have to replace all the spaces that
you do want to match in the regexp by [ ], \s or \s+:

$array_11 =~ m{By \s+ Steve \s+ (.*), \s+
               MarketWatch \s+ LONDON \s+ (.*) \s+
               -- \s+ Shares \s+ of \s+ Anglo \s+ American \s+ 
               rallied \s+ on \s+ Monday \s+ morning \s+ as \s+ 
               (.*) \s+ bet \s+ that \s+ the \s+ mining \s+ 
               group \s+ will \w+ reject \w+ a \w+(.*)
              }x;

So in fact I would probably write something like this:

my $sentence= q{By Steve (.*), MarketWatch LONDON (.*) }
            . q{-- Shares of Anglo American rallied on Monday morning as (.*) }
            . q{bet that the mining group will reject a (.*)}
            ;
my $array_11=~ m{$sentence};

A last comment: $array_11 has a strong code smell, if it's an array, then make it an array, not several scalar variables.

滴情不沾 2024-08-01 07:47:23

您可能正在寻找 /x 修饰符。

来自 perldoc perlre

x 通过允许空格和注释来扩展模式的易读性。

You may be looking for the /x modifier.

From perldoc perlre:

x Extend your pattern's legibility by permitting whitespace and comments.

独享拥抱 2024-08-01 07:47:23

所有逃离的空间都非常丑陋且令人分心。 所以,这里有一个替代方案:

my ($pattern) = map { qr/$_/ } join q{ }, split q{ }, <<'EOP';
    Steve (.*), MarketWatch LONDON (.*) --
    Shares of Anglo American rallied on Monday morning
    as (.*) bet that the mining group will \w+ reject
    \w+ a \w+(.*)
EOP

$text =~ $pattern;

注意:我留下了 (.*) ,因为我不知道 OP 想要什么,但请参阅 Axeman 对 米罗德的回答

All the escaped spaces are really ugly and distracting. So, here is an alternative:

my ($pattern) = map { qr/$_/ } join q{ }, split q{ }, <<'EOP';
    Steve (.*), MarketWatch LONDON (.*) --
    Shares of Anglo American rallied on Monday morning
    as (.*) bet that the mining group will \w+ reject
    \w+ a \w+(.*)
EOP

$text =~ $pattern;

NB: I left the (.*) in because I did not know what the OP wants, but see Axeman's comment on mirod's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文