2013-02-19 78 views
-4

我並不是最大的正則表達式,因此將它們從一個lang轉換爲另一個lang可能是一項艱鉅的任務。將Python正則表達式轉換爲Perl

tre = re.compile("///--STRING TEXT ONE. Ends with the word EDIT.(?:.*)--///(?:(?:.*\n))*///--END is the first word in STRING TEXT TWO--///") 
result = tre.sub(motionBlur_text, configContents) 

如果你們可以幫我搬這個到perl(這是我聽到的是更好地爲reg表達式中),這將是非常酷。

+0

正則表達式庫可能不是一個很好的理由。 Python正則表達式很棒。我不知道Perl,但如果它好得多,我會感到驚訝。 – Joe 2013-02-19 15:43:56

+0

你在做什麼,你需要Perl的正則表達式? – nhahtdh 2013-02-19 15:44:25

+2

@Joe:Perl正則表達式支持Unicode字符匹配(我主要指的是'\ p'),而Python're'缺少支持。除此之外,我認爲Python're'對於大多數目的來說還是可以的。 Python're2'包確實支持這些,但是相當強大(我認爲它在Perl中支持相當多的花裏胡哨的功能)。 – nhahtdh 2013-02-19 15:47:45

回答

2

在Perl正則表達式是相同的:

my $re = qr#///--STRING TEXT ONE. Ends with the word EDIT.(?:.*)--///(?:(?:.*\n))*///--END is the first word in STRING TEXT TWO--///#; 
0

編譯通過qr//操作完成。你可以使用(主要是任何)字符而不是'/'。因爲這就是Perl的最佳實踐建議,我會使用大括號:

my $tre = qr{///--STRING TEXT ONE. Ends with the word EDIT.(?:.*)--///(?:(?:.*\n))*///--END is the first word in STRING TEXT TWO--///}; 

要執行就地取代(見Regexp Quote-Like Operators):

$motionBlur_text =~ s/$tre/$configContents/g; 

要在字符串的副本進行替換,並返回它(Perl 5.14+)

my $result = $motionBlur_text =~ s/$tre/$configContents/gr; 
相關問題