gpt4 book ai didi

regex - 用于提取多行 block 的 perl 正则表达式

转载 作者:行者123 更新时间:2023-12-04 07:56:58 26 4
gpt4 key购买 nike

我有这样的文字:

00:00 stuff
00:01 more stuff
multi line
and going
00:02 still
have


所以,我没有一个块结束,只有一个新的块开始。

我想递归获取所有块:

1 = 00:00 stuff
2 = 00:01 more stuff
multi line
and going


等等

下面的代码只给了我这个:
$VAR1 = '00:00';
$VAR2 = '';
$VAR3 = '00:01';
$VAR4 = '';
$VAR5 = '00:02';
$VAR6 = '';

我究竟做错了什么?

my $text = '00:00 stuff
00:01 more stuff
multi line
and going
00:02 still
have
';
my @array = $text =~ m/^([0-9]{2}:[0-9]{2})(.*?)/gms;
print Dumper(@array);

最佳答案

引入 5.10.0 版本 named capture groups这对于匹配非平凡的模式很有用。

(?'NAME'pattern)
(?<NAME>pattern)

A named capture group. Identical in every respect to normal capturing parentheses () but for the additional fact that the group can be referred to by name in various regular expression constructs (such as \g{NAME}) and can be accessed by name after a successful match via %+ or %-. See perlvar for more details on the %+ and %- hashes.

If multiple distinct capture groups have the same name then the $+{NAME} will refer to the leftmost defined group in the match.

The forms (?'NAME'pattern) and (?<NAME>pattern) are equivalent.



命名捕获组允许我们在正则表达式中命名子模式,如下所示。
use 5.10.0;  # named capture buffers

my $block_pattern = qr/
(?<time>(?&_time)) (?&_sp) (?<desc>(?&_desc))

(?(DEFINE)
# timestamp at logical beginning-of-line
(?<_time> (?m:^) [0-9][0-9]:[0-9][0-9])

# runs of spaces or tabs
(?<_sp> [ \t]+)

# description is everything through the end of the record
(?<_desc>
# s switch makes . match newline too
(?s: .+?)

# terminate before optional whitespace (which we remove) followed
# by either end-of-string or the start of another block
(?= (?&_sp)? (?: $ | (?&_time)))
)
)
/x;

像这样使用它
my $text = '00:00 stuff
00:01 more stuff
multi line
and going
00:02 still
have
';

while ($text =~ /$block_pattern/g) {
print "time=[$+{time}]\n",
"desc=[[[\n",
$+{desc},
"]]]\n\n";
}

输出:

$ ./blocks-demo
时间=[00:00]
描述=[[[[
东西
]]]

时间=[00:01]
描述=[[[[
更多东西
多线

]]]

时间=[00:02]
描述=[[[[
仍然

]]]

关于regex - 用于提取多行 block 的 perl 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com