gpt4 book ai didi

perl - For 循环如何在 perl 中工作

转载 作者:行者123 更新时间:2023-12-01 09:32:04 25 4
gpt4 key购买 nike

#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
s/\w<([^>]+)>/\U$1/g;
print;
}

表达式s/\w<([^>]+)>/\U$1/g;工作吗?

最佳答案

替换是这样做的:

s/             
\w< # look for a single alphanumeric character followed by <
([^>]+) # capture one or more characters that are not <
> # followed by a >
/ ### replace with
\U # change following text to uppercase
$1 # the captured string from above
/gx # /g means do this as many times as possible per line

我添加了 /x 修饰符以便能够可视化正则表达式。字符类 [^>] 被取反,由 [ 后面的 ^ 字符表示,表示“除了 之外的任何字符>”。

例如,在 perldoc 命令的输出中

X<atan2> X<arctangent> X<tan> X<tangent>

改为

ATAN2 ARCTANGENT TAN TANGENT

关于perl - For 循环如何在 perl 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14209451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com