You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl脚本提取单引号间指定格式数据需求及求助

Perl脚本修正方案:提取指定格式条目

处理逻辑

  1. 将单行输入按单引号分割,定位每个独立条目
  2. 按要求顺序匹配5类目标格式,收集符合条件的条目
  3. 移除条目前后单引号,最终用|分隔输出结果

修正后的完整脚本

#!/usr/bin/perl
use strict;
use warnings;

# 读取输入(可替换为直接赋值的示例数据)
my $input = <STDIN>;
chomp $input;

my @result;

# 1. 提取必存在的VAI+I:KK+CH格式条目
if ($input =~ /'VAI\+I:KK\+CH:[^']+'/) {
    my $item = $&;
    $item =~ s/^'|'$//g;
    push @result, $item;
}

# 2. 提取含TKN:402的LRI条目
if ($input =~ /'LRI[^']+TKN:402[^']+'/) {
    my $item = $&;
    $item =~ s/^'|'$//g;
    push @result, $item;
}

# 3. 提取含M11I02和14的FSM+E条目
if ($input =~ /'FSM\+E[^']+M11I02[^']+14[^']+'/) {
    my $item = $&;
    $item =~ s/^'|'$//g;
    push @result, $item;
}

# 4. 提取含TKN:816的LRI条目
if ($input =~ /'LRI[^']+TKN:816[^']+'/) {
    my $item = $&;
    $item =~ s/^'|'$//g;
    push @result, $item;
}

# 5. 提取含M11I02和4的FSM+N条目
if ($input =~ /'FSM\+N[^']+M11I02[^']+4[^']+'/) {
    my $item = $&;
    $item =~ s/^'|'$//g;
    push @result, $item;
}

# 输出结果
print join('|', @result) . "\n";

脚本说明

  • 启用strict和warnings强制规范代码,避免潜在错误
  • 正则表达式精准匹配单引号包裹的目标条目,[^']+确保只匹配当前条目的内容,不会跨条目匹配
  • 严格按照需求顺序收集结果,保证输出顺序符合要求
  • 支持从标准输入读取数据,可通过管道或文件重定向使用,比如:
    cat input.txt | perl extract.pl
    

内容的提问来源于stack exchange,提问作者Sandeep Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:31:15