加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在perl脚本中替换变量选项

发布时间:2020-12-15 23:35:39 所属栏目:大数据 来源:网络整理
导读:在perl文本中,替换非常简单和强大. 我想做一个带有变量替换的脚本,例如: if ( $IgnoreCase ) {$opt = "gi"} else {$opt = "g"}$string =~ s/$source/$replace/$opt; 结果是: Scalar found where operator expected … 是否有可能做选项变量? 解决方法 因
在perl文本中,替换非常简单和强大.
我想做一个带有变量替换的脚本,例如:

if ( $IgnoreCase ) {$opt = "gi"} else {$opt = "g"}

$string =~ s/$source/$replace/$opt;

结果是:

Scalar found where operator expected …

是否有可能做选项变量?

解决方法

因为你在所有情况下都可以使用/ g,

my $opt = $IgnoreCase ? "(?i)" : "";

$string =~ s/$opt$source/$replace/g;

关于这个主题的更多内容在perldoc perlre

One or more embedded pattern-match modifiers,to be turned on (or turned off if preceded by “-” ) for the remainder of the pattern or the remainder of the enclosing pattern group (if any).

This is particularly useful for dynamically-generated patterns,such as those read in from a configuration file,taken from an argument,or specified in a table somewhere. Consider the case where some patterns want to be case-sensitive and some do not: The case-insensitive ones merely need to include (?i) at the front of the pattern.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读