0
我有一個Perl腳本來捕獲數學輸出,但我試圖找到一個好辦法,正好趕上開始與線「走出[1]」。的Perl - 是開頭的正則表達式行「走出[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)***:
~~~~~~~~~~
爲什麼是根(試驗2):空白?
您需要轉義模式中的方括號。如果行的起始處有空格,請添加'\ h *'以匹配它們。您可以使用此工具進行測試:http://regex101.com –