正则表达式 – 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";
 }

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...