我创建了一个简单的 perl 脚本来使用 Regex 并从文本文件中提取值。我已经使用Windows来开发这个脚本并且它在Windows中运行良好
use strict;
use warnings;
use File::Slurp;
my $content = read_file("config.ini");
my $lin_targetdir = $1 if $content =~ m/targetDirectory\s*=\s*(.*)/;
print($lin_targetdir); # /opt/mypath in linux i am getting this output
我的配置是
[prdConfig]
IP = xxxxxx
UserID = yyyyyy
password = "zzzzzzz"
targetDirectory = /opt/mypath
但是,当我在 Linux (centos 7) 中运行上述内容时,脚本不会打印该值。我的代码出了什么问题?你能帮我吗?
最佳答案
用正则表达式本身排除回车符:
/targetDirectory\s*=\s*(\V*)/
As Perl 5.10 specified in the documentation,
\V
is the same as[^\n\cK\f\r\x85\x{2028}\x{2029}]
and shouldn't match any of\n
,\r
or\f
, as well asCtrl+(Control char)
(*nix),0x85
,0x2028
and0x2029
.
替代方案:
/targetDirectory\s*=\s*([^\r\n]*)/
仅排除换行符和回车符。
关于regex - Windows 和 Linux 中的 Perl 正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68692065/