gpt4 book ai didi

Perl:如何提取括号之间的字符串

转载 作者:行者123 更新时间:2023-12-05 09:24:29 24 4
gpt4 key购买 nike

我有一个 moinmoin 文本格式的文件:

* [[  Virtualbox Guest Additions]] (2011/10/17 15:19)
* [[ Abiword Wordprocessor]] (2010/10/27 20:17)
* [[ Sylpheed E-Mail]] (2010/03/30 21:49)
* [[ Kupfer]] (2010/05/16 20:18)

“[[”和“]]”之间的所有单词都是条目的简短描述。我需要提取整个条目,而不是每个单词。

我在这里找到了类似问题的答案:https://stackoverflow.com/a/2700749/819596但无法理解答案:"my @array = $str =~/(\{ (?: [^{}]* | (?0) )*\} )/xg;"

任何有效的东西都会被接受,但解释会有很大帮助,即:(?0)/xg 的作用。

最佳答案

代码大概是这样的:

use warnings; 
use strict;

my @subjects; # declaring a lexical variable to store all the subjects
my $pattern = qr/
\[ \[ # matching two `[` signs
\s* # ... and, if any, whitespace after them
([^]]+) # starting from the first non-whitespace symbol, capture all the non-']' symbols
]]
/x;

# main processing loop:
while (<DATA>) { # reading the source file line by line
if (/$pattern/) { # if line is matched by our pattern
push @subjects, $1; # ... push the captured group of symbols into our array
}
}
print $_, "\n" for @subjects; # print our array of subject line by line

__DATA__
* [[ Virtualbox Guest Additions]] (2011/10/17 15:19)
* [[ Abiword Wordprocessor]] (2010/10/27 20:17)
* [[ Sylpheed E-Mail]] (2010/03/30 21:49)
* [[ Kupfer]] (2010/05/16 20:18)

如我所见,你需要的可以描述如下:在文件的每一行中尝试找到这个符号序列......

[[, an opening delimiter, 
then 0 or more whitespace symbols,
then all the symbols that make a subject (which should be saved),
then ]], a closing delimiter

如您所见,此描述很自然地转换为正则表达式。唯一可能不需要的是 /x 正则表达式修饰符,它允许我对它进行广泛的注释。 )

关于Perl:如何提取括号之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271040/

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