【问题标题】:Store the tokens after splitting拆分后存储令牌
【发布时间】:2015-08-06 00:42:59
【问题描述】:

我有以下 Perl 语句,它通过分隔符 |、\ 或 / 分割字符串

@example = split(/[\|\\\/]/,$i);

拆分后的token如何存储?

例如输入:

约翰|玛丽/马修

我得到的是:

(约翰、玛丽、马修)

我想要什么:

(约翰,|,玛丽,/,马修)

【问题讨论】:

    标签: perl split token


    【解决方案1】:

    在您的正则表达式中放置一个捕获组以保存分隔符:

    my $str = 'John|Mary/Matthew';
    
    my @example = split /([\|\\\/])/, $str;
    
    use Data::Dump;
    dd @example;
    

    输出:

    ("John", "|", "Mary", "/", "Matthew")
    

    这记录在最后一段中:http://perldoc.perl.org/functions/split.html

    【讨论】:

    • 这称为“分隔符保留模式”
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 2019-02-01
    • 2016-02-18
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2014-09-19
    相关资源
    最近更新 更多