Perl - 以“Out [1] =”开头的Regex行

时间:2015-06-08 21:24:55

标签: regex perl wolfram-mathematica

我有一个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(测试2):空白?

1 个答案:

答案 0 :(得分:2)

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

我建议您阅读正则表达式教程like this one来学习此类内容。