Perl - 以 "Out[1]=" 开头的正则表达式行

Perl - Regex line that starts with "Out[1]="

我有一个 Perl 脚本来捕获 Mathematica 输出,但我正在尝试找到一种好方法来捕获以 "Out[1]" 开头的行。

我以为我会检查 if ( $line =~ /^Out[1]=/ )

我做错了什么?下面是代码,下面是程序的输出:

#!/usr/bin/perl
#perl testMathy.pl
use strict;
use warnings;

### Test 1: Print entire Mathematica output from calling Roots[]
my @charPoly = "-R^3 + R^2 + 3*R - 3";
my @roots = `echo \" Roots[@charPoly == 0, R] \" | math`;

print "\n***ROOTS (TEST 1)***: [@roots]\n~~~~~~~~~~\n";

### Test 2: Print line beginning with Out[1]
my $rootList = "";

for my $line ( @roots ){
        #take line that starts with Out[1]
        print "LINE:", $line;
        if ( $line =~ /^Out[1]=/ ){
            print "success\n";
            $rootList .= $line;
            last; #break
        }
    }

print "\n***ROOTS (TEST 2)***: $rootList\n~~~~~~~~~~\n";

输出:

***ROOTS (TEST 1)***: [Mathematica 10.1.0 for Linux x86 (64-bit)
 Copyright 1988-2015 Wolfram Research, Inc.

 In[1]:= 
 Out[1]= R == Sqrt[3] || R == -Sqrt[3] || R == 1

 In[2]:= 
]
~~~~~~~~~~
LINE:Mathematica 10.1.0 for Linux x86 (64-bit)
LINE:Copyright 1988-2015 Wolfram Research, Inc.
LINE:
LINE:In[1]:= 
LINE:Out[1]= R == Sqrt[3] || R == -Sqrt[3] || R == 1
LINE:
LINE:In[2]:= 

***ROOTS (TEST 2)***: 
~~~~~~~~~~

为什么 ROOTS(TEST 2): 空白?

方括号是正则表达式中的特殊字符。尝试:$line =~ /^Out\[1\]=/

我建议阅读正则表达式教程like this one来学习这种东西。