gpt4 book ai didi

perl - perl split() 中的分隔符保留模式

转载 作者:行者123 更新时间:2023-12-02 03:16:05 25 4
gpt4 key购买 nike

split /PATTERN/,EXPR

我在书中读到以下内容,

When you use a pattern in split, be sure to avoid memory parantheses in the pattern since these trigger seperator retention mode.

我似乎找不到详细解释这一点的文档。有人可以简要解释一下分隔符保留模式及其可能的用法吗?

最佳答案

这记录在perldoc -f split中接近尾声(代码中的注释是我自己的):

If the PATTERN contains capturing groups, then for each separator, an additional field is produced for each substring captured by a group (in the order in which the groups are specified, as per backreferences); if any group does not match, then it captures the undef value instead of a substring. Also, note that any such additional field is produced whenever there is a separator (that is, whenever a split occurs), and such an additional field does not count towards the LIMIT. Consider the following expressions evaluated in list context (each returned list is provided in the associated comment):

split(/-|,/, "1-10,20", 3)       # ('1', '10', '20')
# No retention, '-', ',' consumed

split(/(-|,)/, "1-10,20", 3) # ('1', '-', '10', ',', '20')
# Split on and retain '-' or ','
# 5 elements returned

split(/-|(,)/, "1-10,20", 3) # ('1', undef, '10', ',', '20')
# undef because '-' matches

split(/(-)|,/, "1-10,20", 3) # ('1', '-', '10', undef, '20')
# undef because ',' matches

split(/(-)|(,)/, "1-10,20", 3) # ('1', '-', undef, '10', undef, ',', '20')
# one match per capturing group. (-) matches -, but
# (,) returns undef on trying to match -.
# 7 elements (!)
<小时/>

因此,有两个有趣的怪癖可能会引起粗心的人的注意:

  • 只要捕获组不匹配,但 PATTERN 中的其他内容匹配,就会在列表上下文中生成 undef

  • 您可以使用捕获组进行拆分,并将 LIMIT 指定为 $n,结果列表的内容会超过 $n元素

关于perl - perl split() 中的分隔符保留模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12500887/

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