Perl regex in windows and linux issue

Viewed 123

I have create a simple perl script to use Regex and extract a value from the text file. I have used windows to develop this script and it is working fine in 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

and my config is

[prdConfig]
IP = xxxxxx
UserID = yyyyyy
password = "zzzzzzz"
targetDirectory = /opt/mypath

however when i run the above in Linux (centos 7) the script is not printing the value. What went wrong in my code ? Can you please help me ?

1 Answers

Exclude the carriage return with the regex itself:

/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.

Alternative:

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

to exclude line feed and carriage return characters only.

Related