简体   繁体   中英

Perl: Why is this expression not working with the new version of Perl?

I am attempting to run a Perl script, which produces the following error:

Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/^(. )\\$ENV{ <-- HERE (. )}(.*)$/ at /root/cesm1_2_0/scripts/ccsm_utils/Case.template/ConfigCase.pm line 1180. Compilation failed in require at ./create_newcase line 361

The offending code is:

if($text =~/^(.*)\$ENV{(.*)}(.*)$/){
    ...
}

There was an issue earlier in the code that was due to "using a newer Perl version than that code supports" (see: https://bb.cgd.ucar.edu/machine-configuration-and-generating-domain-file ), so I expect that the regex syntax in Perl has changed.

Can someone translate this line to be compatible with the current version of Perl?

{ is now always treated as a regex metacharacter. When an unescaped character is illegal, you make it legal by escaping it.

$text =~/^(.*)\$ENV\{(.*)}(.*)$/
                   ^---- new character

See https://metacpan.org/pod/distribution/perl/pod/perl5260delta.pod#Unescaped-literal-"{"-characters-in-regular-expression-patterns-are-no-longer-permissible

尝试这个 :

if ($text =~/^(.*)\$ENV\{(.*)\}(.*)$/) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM