windows 和 linux 问题中的 Perl 正则表达式

Perl regex in windows and linux issue

我已经创建了一个简单的 perl 脚本来使用正则表达式并从文本文件中提取一个值。我已经使用 windows 开发了这个脚本,它在 windows

中运行良好
use strict;
use warnings;
use File::Slurp;

my $content = read_file("config.ini");

my $lin_targetdir =  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) 中执行上述操作时,脚本不会打印该值。我的代码出了什么问题?你能帮帮我吗?

使用正则表达式本身排除回车 return:

/targetDirectory\s*=\s*(\V*)/

Reference:

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 as Ctrl+(Control char) (*nix), 0x85, 0x2028 and 0x2029.

选择:

/targetDirectory\s*=\s*([^\r\n]*)/

仅排除换行符和回车符 return 个字符。