【发布时间】:2012-12-11 02:44:42
【问题描述】:
到目前为止,我已经这样做了,但仍然无法正常工作。
open (GOOD_FILE,"<$pre.XXX.XXX$post") or die "Cannot open XXX";
# temporarily undefine the input line separator to read the entire file at once:
my $text = do { local $/; <GOOD_FILE> };
close GOOD_FILE or die $!;
#File name to replace XXXX.XXXXX.XXXX.XXXX.CCCC where last CCCC can be anything
$text =~ s/^(.*\n)?XXXX\.XXXX\.XXXX\.[^\n]*/$1myname/s;
open (GOOD_FILE,"<$pre.XXXX.XXXX$post") or die "Cannot open XXXX";
print GOOD_FILE $text;
close GOOD_FILE or die $!;
输入文件如下所示:
I am tester testing
I am still learning.
XXXX.XXXX.XXXX.XXXX.CCCC
I am tester.
XXXX.XXXX.XXXX.XXXX.PPPP
它应该产生这个输出:
I am tester testing
I am still learning.
XXXX.XXXX.XXXX.XXXX.CCCC
I am tester.
myname.
但我得到的是空文件。
谢谢你们,我能够使用变量来解决问题,我只是将正则表达式作为变量,它工作正常。
【问题讨论】:
标签: string perl replace pattern-matching last-occurrence