微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – Perl提取文本

我一直在研究这个问题!
我很感激你的帮助……

我的文档会是什么样子:

<text>
<text> command <+>= "stuff_i_need" <text>
<text>
<text> command <+>= stuff <text>
<text>
<text> command <+>= -stuff <text>
<text>

>任何带有缠绕括号的东西都是可选的
>东西可以是任何东西(苹果,橙子,香蕉),但它是我需要提取的东西
>命令是固定的

我的代码到目前为止:

#!/usr/bin/env perl

use warnings;
use strict;
use Text::Diff;

# File Handlers 
open(my $ofh,'>in.txt');
open(my $ifh,'<out.txt');

while (<$ifh>)
{
    # Read in a line
    my $line = $_;
    chomp $line;

    # Extract stuff
    my $extraction = $line;

    if ($line =~ /command \+= /i) {        
        $extraction =~ s/.*"(.*)".*/$1/;
        # Write to file
        print $ofh "$extraction\n";
    }
}

解决方法

根据示例输入:

if ($line =~ /command\d*\s*\+?=\s*["-]?(\w+)"?/i) {    
    $extraction = $1; 
    print "$extraction\n";
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐