regex - Windows 和 Linux 中的 Perl 正则表达式问题

标签 regex linux perl

我创建了一个简单的 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*)/

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]*)/

仅排除换行符和回车符。

关于regex - Windows 和 Linux 中的 Perl 正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68692065/

相关文章:

javascript - 如何将 "camelCase"转换为 "Camel Case"?

python正则表达式,空格之间只有数字

linux - 解压目录和子目录中的所有 .gz

perl - 使用 Perl 的 Win32::Registry 时如何修复 `"main::HKEY_LOCAL_MACHINE"used once once` 警告?

Perl expect - 如何控制目标机器上的超时

perl - 一个perl命令行,打印用于运行脚本的perl版本

python - python 中的 re.search() 进入无限循环

java - Java 中点分隔字符串的模式匹配

java - 使用 build.sh 脚本构建 Android

linux - 基于每个连接的 TCP 参数调整